Skip to content

Commit c7459c8

Browse files
authored
Create find-all-lonely-numbers-in-the-array.cpp
1 parent f0e3492 commit c7459c8

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Time: O(n)
2+
// Space: O(n)
3+
4+
// freq table
5+
class Solution {
6+
public:
7+
vector<int> findLonely(vector<int>& nums) {
8+
unordered_map<int, int> cnt;
9+
for (const auto& x : nums) {
10+
++cnt[x];
11+
}
12+
vector<int> result;
13+
for (const auto& x : nums) {
14+
if (cnt[x] == 1 && !cnt.count(x - 1) && !cnt.count(x + 1)) {
15+
result.emplace_back(x);
16+
}
17+
}
18+
return result;
19+
}
20+
};

0 commit comments

Comments
 (0)