Q:

Ruby program to remove duplicate elements from the array

belongs to collection: Ruby Arrays Programs

0

In this program, we will create an array of integers. Then we will remove duplicate elements from the array using uniq() method.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Ruby Arrays Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Ruby program to demonstrate the Array.concat() met... >>
<< Ruby program to demonstrate the Array.collect() me...