217. Contains Duplicate
Problem Statement
- Given an integer array
nums
, return true
if any value appears at least twice in the array, and return false
if every element is distinct.
Approach
- Set -> if set size == vector size (no duplicates) ; else duplicates
- HashMap -> insert elements with value increment for each entry.
- Iterate map manually for all keys using ForEach, if any key has >1 value; return true else false
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
unordered_set<int> st;
for(auto a : nums)
st.insert(a);
if(st.size() == nums.size())
return false;
return true;
}
};