---
# System prepended metadata

title: LeetCode 217. Contains Duplicate
tags: [Leetcode]

---

---
title: 'LeetCode 217. Contains Duplicate'
disqus: hackmd
---

# LeetCode 217. Contains Duplicate



## Description

Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.

## Example

Input: nums = [1,2,3,1]
Output: true

Input: nums = [1,1,1,3,3,4,3,2,4,2]
Output: true

## Constraints

1 <= nums.length <= 10^5^
-10^9^ <= nums[i] <= 10^9^

## Answer

此題可先排序再抓出若有前後相等的就直接return true，否則return false。


```Cin=
//2022_03_12
int comp(const void *a,const void *b){
    return *(int*)a - *(int*)b;
}
bool containsDuplicate(int* nums, int numsSize){
    qsort(nums,numsSize,sizeof(int),comp);
    for(int i = 1; i<numsSize; i++){
        if(nums[i-1] == nums[i]){return true;}
    }
    return false;
}
```



## Link
https://leetcode.com/problems/contains-duplicate/
###### tags: `Leetcode` 
