Ruby program to remove duplicate elements from the array

Ruby Example: Write a program to remove duplicate elements from the array.
Submitted by Nidhi, on January 16, 2022

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 »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.