Skip to content

Commit bc13226

Browse files
committed
update 287
1 parent d157b8a commit bc13226

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

0287-find-the-duplicate-numer.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@
33
44
Submitted: January 5, 2024
55
6-
Runtime: 63 ms (beats 20.20%)
7-
Memory: 87.86 MB (beats 15.89%)
6+
Runtime: 71 ms (beats 15.48%)
7+
Memory: 87.77 MB (beats 18.81%)
88
*/
99

1010
class Solution {
1111
public:
12-
int findDuplicate(vector<int>& nums) {
13-
unordered_map<int, int> map;
12+
int findDuplicate(const vector<int>& nums) const noexcept {
13+
unordered_set<int> set;
1414
for (const int n : nums) {
15-
map[n]++;
16-
if (map[n] == 2) return n;
15+
if (set.find(n) == set.end()) {
16+
set.emplace(n);
17+
} else {
18+
return n;
19+
}
1720
}
1821
return 0;
1922
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy