Skip to content

Commit 11247dd

Browse files
authored
Add files via upload
1 parent 1020d48 commit 11247dd

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

217.Contains Duplicate.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include <unordered_set>
2+
#include <vector>
3+
4+
using namespace std;
5+
6+
class Solution {
7+
public:
8+
bool containsDuplicate(vector<int>& nums) {
9+
unordered_set<int> seen;
10+
11+
for (int num : nums) {
12+
if (seen.count(num)) return true; // Found a duplicate
13+
seen.insert(num);
14+
}
15+
16+
return false; // No duplicates found
17+
}
18+
};
19+
20+
21+
22+
23+
24+
25+
26+
class Solution {
27+
public:
28+
bool containsDuplicate(vector<int>& nums) {
29+
sort(nums.begin(), nums.end()); // O(n log n)
30+
31+
for (int i = 1; i < nums.size(); i++) {
32+
if (nums[i] == nums[i - 1]) return true;
33+
}
34+
35+
return false;
36+
}
37+
};
38+
39+
40+
41+
42+
43+
44+
class Solution {
45+
public:
46+
bool containsDuplicate(vector<int>& nums) {
47+
sort(nums.begin(), nums.end()); // O(n log n)
48+
49+
int left = 0;
50+
int right = 1; // Start from second element
51+
52+
while (right < nums.size()) {
53+
if (nums[left] == nums[right]) {
54+
return true; // Duplicate found
55+
}
56+
left++;
57+
right++;
58+
}
59+
60+
return false; // No duplicates
61+
}
62+
};

0 commit comments

Comments
 (0)