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 ele ment is distinct.
Example 1:
Input: [1,2,3,1]output:true
Example 2:
Input: [1,2,3,4]output:false
Example 3:
Input: [1,1,1,3,3,4,3,2,4,2]output:true
Given an array of integers, determine if there are duplicate elements.
If any value occurs at least two times in the array, the function returns True. Returns False if each element in the array is not the same.
Example 1:
Input: [1,2,3,1] Output: True
Example 2:
Input: [1,2,3,4] Output: false
Example 3:
Input: [1,1,1,3,3,4,3,2,4,2] Output: True
1 classSolution {2Func Containsduplicate (_ Nums: [Int])Bool {3 ifNums = = Nil | | Nums.count = =0{return false}4 varArray:[int] =Nums5 //Sort First6Array.Sort () {$0< $1}7 //then traverse8 forIinch 0.. < (array.count-1)9 {Ten ifArray[i] = = array[i+1] One { A return true - } - } the return false - } -}
36ms
1 classSolution {2Func Containsduplicate (_ Nums: [Int])Bool {3 var Set= set<int>()4 5 forIndexinchNums.indiceswhere!Set. Insert (Nums[index]). inserted{6 return true7 }8 return false9 }Ten}
40ms
1 class Solution {2 func containsduplicate (_ Nums: [Int]), Bool {3 var Set = Set (nums)4 returnset. Count! = Nums.count 5 }6 }
217. Duplicate elements exist | Contains Duplicate