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 class Solution {
 2 public:
 3     bool containsDuplicate(vector<int>& nums) {
 4         map<int,int> hmap;
 5         int n=nums.size();
 6         if(n<2) return false;
 7         for(int i=0;i<n;i++)
 8         {
 9             if(hmap.count(nums[i]))
10                 return true;
11             else
12                 hmap[nums[i]]=i;
13         }
14         return false;
15     }
16 };

 

posted @ 2015-07-29 16:17  鸭子船长  阅读(137)  评论(0编辑  收藏  举报