Leetcode: 217. Contains Duplicate
- Primary idea: Use a hash table to count frequency of all numbers in the array.
- Time Complexity: O(n)
- Space Complexity: O(n)
func containsDuplicate(_ nums: [Int]) -> Bool {
var dict = [Int: Int]()
for num in nums {
if dict[num] != nil {
return true
}
dict[num] = 1
}
return false
}
containsDuplicate([1,2,3,1]) // true
containsDuplicate([1,2,3,4]) // false
containsDuplicate([1,1,1,3,3,4,3,2,4,2]) // true