217. Contains Duplicate--LeetCode Record

Tong_hdj發表於2016-07-13

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.

    func containsDuplicate(nums: [Int]) -> Bool {
        var records:[Int:Int] = [:]
        for num in nums {
            if (records[num] != nil) {
                return true
            }else {
                records[num] = 1
            }
        }
        return false
    }

相關文章