Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.
Possible Solution
Key Concept–SET
Python
class Solution--Just one line(object):
def containsDuplicate(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
return len(set(nums)) < len(nums)
Java Version
public class Solution {
public boolean containsDuplicate(int[] nums) {
final Set<Integer> temp = new HashSet<Integer>();
for (int num : nums) {
if (temp.contains(num)) return true;
temp.add(num);
}
return false;
}
}