Home »
Ruby Tutorial »
Ruby Programs
Ruby program to remove duplicate elements from the array
Last Updated : December 15, 2025
Problem Solution
In this program, we will create an array of integers. Then we will remove duplicate elements from the array using uniq() method.
Program/Source Code
The source code to remove duplicate elements from the array is given below. The given program is compiled and executed successfully.
# Ruby program to remove duplicate
# elements from the array
arr = [10,20,30,40,50,30,10];
res = arr.uniq();
puts "Array elements: \n",arr,"\n";
puts "Uniq Array elements: \n",res,"\n";
Output
Array elements:
10
20
30
40
50
30
10
Uniq Array elements:
10
20
30
40
50
Explanation
In the above program, we created an array arr with some elements. Then we used uniq() method. The uniq() method is used to remove duplicate elements from array and return an array with unique elements. After that, we printed unique elements.
Ruby Arrays Programs »
Advertisement
Advertisement