Skip to content

Commit b00545f

Browse files
authored
Merge pull request #2391 from u-siop/main
[u-siop] WEEK 01 solutions
2 parents 360c039 + df862dd commit b00545f

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

contains-duplicate/u-siop.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
bool containsDuplicate(vector<int>& nums) {
4+
// instinct : sort the array and check all the array whether duplicate or not
5+
// time complexity : NlogN(sort) + N(check all the array)
6+
// space complexity : 1
7+
8+
// how do we, in real world, judge there is a duplicated element, definitely need a reference( another data strucrue) to check
9+
10+
unordered_map<int, bool> seen; // using hash map for updating, already have seen the element
11+
12+
for (const auto& i : nums) {
13+
if (seen[i] >= 1)
14+
return true;
15+
seen[i] = true;
16+
}
17+
18+
return false;
19+
}
20+
};

0 commit comments

Comments
 (0)