LeetCode -- Contains Duplicate
題目描述:
Contains Duplicate
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.
判斷序列是否存在重復。
算是哈希表的基本使用。
實現代碼:
public bool ContainsDuplicate(int[] nums) {
var hash = new Dictionary();
for(var i = 0;i < nums.Length; i++){
if(!hash.ContainsKey(nums[i])){
hash.Add(nums[i],true);
}
else{
return true;
}
}
return false;
}