w3resource

Ruby Array Exercises: Remove duplicate elements from a given array

Ruby Array: Exercise-6 with Solution

Write a Ruby program to remove duplicate elements from a given array.

Ruby Array Exercises: Remove duplicate elements from a given array

Ruby Code:

nums = [1, 2, 3, 4, 1, 2, 2, 3, 5, 6]
print "Original array:\n"
print nums
print "\n Array with unique elements:\n"
new_nums = nums.uniq
print new_nums

Output:

Original array:
[1, 2, 3, 4, 1, 2, 2, 3, 5, 6]
 Array with unique elements:
[1, 2, 3, 4, 5, 6]

Flowchart:

Flowchart: Remove duplicate elements from a given array

Ruby Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Ruby program to compute the sum of elements in a given array.
Next: Write a Ruby program to check two given arrays of integers and test if they have the same first element or they have the same last element. Both arrays length must be 1 or more.

What is the difficulty level of this exercise?