
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Duplicate in Array of N+1 Integers Using C++
Problem Description
In this problem, we are given an array that contains all unique elements except one element which is present exactly two times in the array. We have to return that element which is present two times in the array. In this article, we are going to learn how we can find duplicates in an array of N+1 integers in C++.
Example
Input: array = {1, 3, 4, 2, 2} Output: 2 Explanation: 2 is the only element in the array that occurs two times.
Approaches to Find Duplicate Element in an Array of N+1 Integers
To solve this problem, here are the three approaches that can you use:
Using Brute Force Approach
This is the simplest approach to finding duplicate element present in an array. In this approach, we use two nested loops to compare each element with every other element in the array. If we find any match, then the number is a duplicate element.
Implementation Code
#include<bits/stdc++.h> using namespace std; int findDuplicateElement(int arr[], int n) { for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (arr[i] == arr[j]) { return arr[i]; } } } return -1; } int main() { int arr[] = {1, 3, 4, 2, 2}; int n = sizeof(arr) / sizeof(arr[0]); cout << "Duplicate element in the array is: " << findDuplicateElement(arr, n) << endl; return 0; }
Output:
Duplicate element is: 2
Time Complexity: O(N2)
Space Complexity: O(1)
Using Sorting Approach
In this approach, we first sort the array. Then we traverse the array, and if two adjacent elements are found to be the same, then that element of the array is a duplicate element.
Implementation Code
#include<bits/stdc++.h> using namespace std; int findDuplicate(int arr[], int n) { sort(arr, arr + n); for (int i = 0; i < n - 1; i++) { if (arr[i] == arr[i + 1]) { return arr[i]; } } return -1; } int main() { int arr[] = {1, 3, 4, 2, 2}; int n = sizeof(arr) / sizeof(arr[0]); cout << "Duplicate element is: " << findDuplicate(arr, n) << endl; return 0; }
Output:
Duplicate element is: 2
Time Complexity: O(N log N)
Space Complexity: O(1)
Using Floyd's Cycle Detection Algorithm
This is the most efficient approach as it uses constant space. In this approach, we use a slow and fast pointer to traverse the array, and where the cycle is found, that element is a duplicate element. We traverse the array using slow and fast pointers to detect the cycle. We first find the intersection point of the two pointers. Once a cycle is detected, one pointer is reset to the start of the array while the other remains at the meeting point. Both pointers then move one step at a time, and the position where they meet again is the duplicate element of the array.
Implementation Code
#include<bits/stdc++.h> using namespace std; int findDuplicateElement(int arr[], int n) { int slow = arr[0]; int fast = arr[0]; do { slow = arr[slow]; fast = arr[arr[fast]]; } while (slow != fast); slow = arr[0]; while (slow != fast) { slow = arr[slow]; fast = arr[fast]; } return slow; } int main() { int arr[] = {1, 3, 4, 2, 2}; int n = sizeof(arr) / sizeof(arr[0]); cout << "Duplicate element is: " << findDuplicateElement(arr, n) << endl; return 0; }
Output:
Duplicate element is: 2
Time Complexity: O(N)
Space Complexity: O(1)