###### tags: `LeetCode` `Easy` # LeetCode #217 [Contains Duplicate](https://leetcode.com/problems/contains-duplicate) ### (Easy) 給定一個整數數組,判斷是否存在重複元素。 如果存在一值在數組中出現至少兩次,函數返回 true 。如果數組中每個元素都不相同,則返回 false 。 --- 刪除重複元素, 比對刪除前後數組大小即可。 --- ``` class Solution { public: bool containsDuplicate(vector<int>& nums) { int n=nums.size(); sort(nums.begin(),nums.end()); nums.erase(unique(nums.begin(),nums.end()),nums.end()); if(n==nums.size()) return 0; return 1; } }; ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up