-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path03_contains-duplicate.cpp
More file actions
42 lines (34 loc) · 911 Bytes
/
03_contains-duplicate.cpp
File metadata and controls
42 lines (34 loc) · 911 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
38
39
40
41
42
// DATE: 27-07-2023
/* PROGRAM: 03_Array - Contains Duplicate
https://leetcode.com/problems/contains-duplicate/
*/
// @ankitsamaddar @2023
#include <ios>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
set<int> hashed;
for (int n : nums) {
if (hashed.count(n)) {
return true;
}
// else hash the current element
hashed.insert(n);
}
// if not found
return false;
}
};
int main() {
int arr[] = {1, 2, 3, 1};
std::vector<int> v(arr, arr + sizeof(arr) / sizeof(int));
Solution sol;
bool ret = sol.containsDuplicate(v);
cout << boolalpha << ret << endl; // boolalpha to print the boolean value instead of
// numeric(0/1)
return 0;
}