soapyigu/LeetCode-Swift

Array -> ContainsDuplicate.swift

phpmaple opened this issue · 3 comments

simple problem:

class Solution {
    func containsDuplicate(nums: [Int]) -> Bool {
        return nums.count > Set(nums).count
    }
}

Awesome solution, thank you

You have to convert the entire array to a set. If you iterate one by one, you can bail out earlier once you detect the first duplicate.

Both solutions have the same complexity, I perfer phpmaple's as it is more concise.