217. 判断是数组否有重复元素 Contains Duplicate

217. 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.


  1. public class Solution {
  2. public bool ContainsDuplicate(int[] nums) {
  3. int length = nums.Length;
  4. int num = 0;
  5. int value =0;
  6. Dictionary<int,int> d = new Dictionary<int,int>();
  7. for (int i = 0; i < length; i++) {
  8. num = nums[i];
  9. if (d.TryGetValue(num, out value)){
  10. return true;
  11. } else {
  12. d[num] = 1;
  13. }
  14. }
  15. return false;
  16. }
  17. }





posted @ 2017-01-10 23:09  xiejunzhao  阅读(118)  评论(0编辑  收藏  举报