Just run a simple hashmap to check for duplicates.
code
class Solution {
public:
vector<int> getSneakyNumbers(vector<int>& nums) {
vector<int> ans;
unordered_set<int> seen;
for (int& num : nums) {
if (!seen.count(num)) {
seen.insert(num);
} else {
ans.push_back(num);
}
}
return ans;
}
};
complexity
- Time:
O(n)- Space:
O(n)
learnings
- u could use binary / math to get
O(1)space complexity but ceebs
time taken
- 44 seconds