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.
Solution:
Idea: sort numbers first, and than check consecutive elements. However, the complexity of sorting depends on sorting algorithm.
class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
if (nums.size() == 0)
return false;
sort(nums.begin(),nums.end());
int valmax = nums[nums.size()-1];
for (int i = 0; i < nums.size() -1; i++){
if ((nums[i] - nums[i+1])==0)
return true;
}
return false;
}
};