Contains Duplicate

Description:

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.

Code:

 1  bool containsDuplicate(vector<int>& nums) {

 2         unordered_map<int,int>m;

 3         for (int i = 0; i < nums.size(); ++i)

 4         {

 5             if (!m.count(nums[i]))

 6                 m[nums[i]]=i;

 7             else

 8                 return true;

 9         }

10         return false;

11     }

 

你可能感兴趣的:(contains)