-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathduplicate.cpp
More file actions
37 lines (32 loc) · 781 Bytes
/
duplicate.cpp
File metadata and controls
37 lines (32 loc) · 781 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//Problem: Given an array, return true if any value appears atleast twice in the array,
// return false if all elements are distinct
#include<bits/stdc++.h>
using namespace std;
bool isDuplicate(vector<int> arr, int n)
{
int i;
sort(arr.begin(), arr.end());
for(i = 1; i < n; i++)
{
if(arr[i] == arr[i-1])
return true;
}
return false;
}
int main()
{
int n, i;
cout << "Enter array of elements : ";
cin >> n;
vector<int> arr(n);
cout << "Enter array elements : " << endl;
for(i = 0; i < n; i++) {
cin >> arr[i];
}
bool res = isDuplicate(arr, n);
if(res)
cout << "Array contains duplicate elements";
else
cout << "Array contains distinct elements";
return 0;
}