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.
这个就用一个Hash表来存,元素为键,次数为值,有大于1的就返回true
/**
* @param {number[]} nums
* @return {boolean}
*/
var containsDuplicate = function(nums) {
var map = {};
var num = nums.length;
for (var i = 0; i < num ; i++) {
if(map[nums[i]]!==undefined) {
map[nums[i]]++;
} else {
map[nums[i]] = 1;
}
if (map[nums[i]]>1)
return true;
}
return false;
};