# 关关的刷题日记05 —— Leetcode 217. Contains Duplicate 方法1和方法2

## 题目

Leetcode 217. Contains Duplicate

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.

## 方法1

```class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
sort(nums.begin(), nums.end());
for(int i=1; i<nums.size(); i++)
{
if(nums[i]==nums[i-1])
return true;
}
return false;
}
};```

```for(int i=0; i<nums.size()-1; i++)
{
if(nums[i]==nums[i+1])```

```class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
sort(nums.begin(), nums.end());
return unique(nums.begin(), nums.end())!=nums.end();
}
};```

## 方法2

```class Solution {
public:
bool containsDuplicate(vector<int>& nums) {
unordered_set<int>t;
for(int x: nums)
{
if(t.find(x)!=t.end())
return true;
else
t.insert(x);
}
return false;
}
};```

0 条评论

30930

33050

24480

11420

43550

27070

32320

20520

32540

21550