iOS Interview - Leetcode 217. Contains Duplicate

June 19, 20241 min read#swift, #interview, #leetcode

Leetcode: 217. Contains Duplicate

  • Primary idea: Use a hash table to count frequency of all numbers in the array.
  • Time Complexity: O(n)
  • Space Complexity: O(n)
func containsDuplicate(_ nums: [Int]) -> Bool {
    var dict = [Int: Int]()
    for num in nums {
        if dict[num] != nil {
            return true
        }
        dict[num] = 1
    }

    return false
}

containsDuplicate([1,2,3,1]) // true
containsDuplicate([1,2,3,4]) // false
containsDuplicate([1,1,1,3,3,4,3,2,4,2]) // true
Quick Drop logo

Profile picture

Personal blog by An Tran. I'm focusing on creating useful apps.
#Swift #Kotlin #Mobile #MachineLearning #Minimalist


© An Tran - 2024