We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent d157b8a commit bc13226Copy full SHA for bc13226
0287-find-the-duplicate-numer.cpp
@@ -3,17 +3,20 @@
3
4
Submitted: January 5, 2024
5
6
-Runtime: 63 ms (beats 20.20%)
7
-Memory: 87.86 MB (beats 15.89%)
+Runtime: 71 ms (beats 15.48%)
+Memory: 87.77 MB (beats 18.81%)
8
*/
9
10
class Solution {
11
public:
12
- int findDuplicate(vector<int>& nums) {
13
- unordered_map<int, int> map;
+ int findDuplicate(const vector<int>& nums) const noexcept {
+ unordered_set<int> set;
14
for (const int n : nums) {
15
- map[n]++;
16
- if (map[n] == 2) return n;
+ if (set.find(n) == set.end()) {
+ set.emplace(n);
17
+ } else {
18
+ return n;
19
+ }
20
}
21
return 0;
22
0 commit comments