forked from mandliya/algorithms_and_data_structures
-
Notifications
You must be signed in to change notification settings - Fork 1
/
containsDuplicate.cpp
43 lines (37 loc) · 1000 Bytes
/
containsDuplicate.cpp
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
38
39
40
41
42
43
/**
* Given an array of integers, find if the array contains any duplicates.
* Your function should return true if any value appears at least twice in the array,
* and it should return false if every element is distinct.
*
* Approach : sort and check for adjacent elements for duplicates.
*
*/
#include <iostream>
#include <vector>
#include <algorithm>
bool containsDuplicate(std::vector<int>& nums) {
std::sort( nums.begin(), nums.end());
for ( size_t i = 1; i < nums.size(); ++i ) {
if ( nums[i-1] == nums[i] ) {
return true;
}
}
return false;
}
void printVect( std::vector<int> & vec ) {
std::cout << "VEC:";
for ( auto & i : vec ) {
std::cout << i << " ";
}
std::cout << std::endl;
}
int main() {
std::vector<int> vec1{ 1, 99, 99, 4, 8, 98, 96, 3, 5, 19, 23, 17, 84, 23 };
printVect(vec1);
if ( containsDuplicate(vec1) ) {
std::cout << "Above vector contains duplicates\n";
} else {
std::cout << "Above vector does not contain duplicates\n";
}
return 0;
}