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 };
联系方式:emhhbmdfbGlhbmcxOTkxQDEyNi5jb20=