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.
Subscribe to see which companies asked this question
用map做的很简单,不赘述
1 class Solution { 2 public: 3 bool containsDuplicate(vector<int>& nums) { 4 map<int,int> M1; 5 int temp; 6 if (nums.empty()) return false; 7 for(auto i=nums.begin();i!=nums.end();i++){ 8 temp=(*i); 9 ++M1[temp]; 10 } 11 for(auto j=M1.begin();j!=M1.end();j++){ 12 if((*j).second>1) return true; 13 } 14 return false; 15 16 } 17 };