Leetcode 217. Contains Duplicate - Solution in Ruby

Revisited on July 25th, 2023


Description

LeetCode Problem 217

Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.


Method structure

This is the method structure provided by Leetcode.

# @param {Integer[]} nums
# @return {Boolean}
def contains_duplicate(nums)
  # TO do
end

Explanation

The problem is asking you to determine whether there are any duplicate values in the given integer array nums.

If there is at least one value that appears more than once, the function should return true. However, if every element in the array is distinct (i.e., no duplicates exist), the function should return false.


Solution steps

Approach

Since there are no constraints that forbid us from using a Ruby built-in function, I think one good approach is to use the uniq method.

We can compare the original array with the other array after removing duplicates. If they differ, it means there are duplicates in the array.


gatinho malhado castanho e preto


Unique elements

# @param {Integer[]} nums
# @return {Boolean}
def contains_duplicate(nums)
    nums.uniq
end

The operation nums.uniq creates a new array that contains only the unique elements from the original nums array.

The time complexity of this operation is O(n), as it requires iterating over all elements of nums to identify and remove duplicates.


Comparison

# @param {Integer[]} nums
# @return {Boolean}
def contains_duplicate(nums)
    nums != nums.uniq
end

The comparison nums != nums.uniq checks whether the original nums array is different from the array containing only the unique elements.

If the two arrays are not equal, it means there were duplicates in the original array, so, it returns true.

Otherwise, if the arrays are equal, it means there were no duplicates, and the condition evaluates to false.

This operation has a constant time complexity of O(1) because it simply compares the references of the two arrays.


True or false

# @param {Integer[]} nums
# @return {Boolean}
def contains_duplicate(nums)
    nums != nums.uniq ? true : false
end

This comparison nums != nums.uniq returns true or false.

The ternary operators ? and : enforce the return.

The ternary expression returns true if there were duplicates in the original array (nums != nums.uniq is true), and false if there were no duplicates (nums != nums.uniq is false).


Final solution

# @param {Integer[]} nums
# @return {Boolean}
def contains_duplicate(nums)
    nums != nums.uniq
end

Since the comparison nums != nums.uniq already returns true or false, it's not necessary to use the ternary expression.


Complexity

This solution checks if the original array is different from the array containing only the unique elements.

The overall time complexity of the contains_duplicate method is determined by the time complexity of nums.uniq, which is linear, also known as O(n).

Since the solution has a linear time complexity, the execution time grows linearly with the size of the input array.


Be happy

You got here. Way to go!

The Office GIF - The Office Happy - Discover & Share GIFs


Let's become friends


Final thoughts

I hope this article helped you. Let me know if you have any questions. Your thoughts, suggestions and corrections are more than welcome. By the way, feel free to drop your suggestions on new blog articles.

Hope to see you next time.