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.
使用两重循环遍历数组,只要一有相同的元素即退出循环返回true,若循环结束跳出循环则返回false
bool containsDuplicate(int* nums, int numsSize) {
for(int i = 0; i < numsSize - 1; i++) {
for(int j = i + 1; j < numsSize; j++) {
if (nums[i] == nums[j])
return true;
}
}
return false;
}