Simple Ruby Sliding Window Solution


  • 0
    R
    def single_non_duplicate(nums)
        return nil if nums.length < 2
        
        left = 0
        right = 1
        
        while(right < nums.length) do
            if nums[left] == nums[right]
                left = right + 1
                right += 1
            else
                return nums[left]
            end
            right += 1
        end
        nums.last
    end
    

Log in to reply
 

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