Contains Duplicate
217. Contains Duplicate
μ£Όμ΄μ§ μ μ λ°°μ΄ nums
μ κ°μ κ°μ΄ λ λ² μ΄μ λνλλ©΄ true
λ₯Ό λ°ννκ³
λͺ¨λ μμκ° κ³ μ ν κ°μ΄λΌλ©΄ false
λ₯Ό λ°ννλλ‘ μμ±νλΌ.
Example 1:
Input: nums = [1,2,3,1]
Output: true
Example 2:
Input: nums = [1,2,3,4]
Output: false
Example 3:
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
Solution
class Solution {
func containsDuplicate(_ nums: [Int]) -> Bool {
return Set(nums).count != nums.count
}
}
μ
λ ₯κ°μΌλ‘ λ€μ΄μ¨ nums
λ₯Ό μ€λ³΅μ΄ μλλ‘ Set
μΌλ‘ κ°μΈμ£Όμμ λμ μμ κ°μμ
μλ³Έ μ
λ ₯κ° nums
μ μμ κ°μκ° μΌμΉνλμ§λ₯Ό λΉκ΅νμλ€.
π Reference
LeetCode-217-ContainsDuplicate