My ruby solution


  • 0
    J

    regular solution

    def contains_duplicate(nums)
        hash = {}
        nums.each do |x|
            return true if hash[x] == x
            hash[x] = x;
        end
        false
    end
    

    one line with Ruby array function(lose meaning, not recommended)

    def contains_duplicate(nums)
        nums.size != nums.uniq.size
    end

  • 0

    What do you mean with "lose meaning"? I find it much easier to understand than your "regular" solution.


  • 3

    Some shorter one-liners:

    def contains_duplicate(nums)
        !!nums.uniq!
    end
    
    def contains_duplicate(nums)
        nums.uniq! != nil
    end
    
    def contains_duplicate(nums)
        nums.uniq != nums
    end
    

  • 0
    J

    I think the target of this question is training algorithm. But the sweet solution won't help.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.