Home » Ruby programming

Array.uniq Method with Example in Ruby

Ruby Array.uniq Method: Here, we are going to learn about the Array.uniq Method with examples in Ruby programming language.
Submitted by Hrithik Chandra Prasad, on February 12, 2020

Array.uniq Method

In this article, we will study about Array.uniq Method. You all must be thinking the method must be doing something related to finding unique elements or objects from the Array instance. It is not as simple as it looks. Well, we will figure this out in the rest of our content. We will try to understand it with the help of syntax and demonstrating program codes.

Method description:

This method is a public instance method and defined for the Array class in Ruby’s library. This method works in a way that it returns a new array that contains all the unique elements from the self-Array instance. If you are providing a block along with the method at the time of its invocation then it will use the return value of block for the comparison. This method will not create an impact on the actual arrangement of objects in the self-Array because this method belongs to the category of non-destructive methods where the changes created by the method are temporary or non-permanent.

Syntax:

    array_instance.uniq -> Array or nil
    or
    array_instance.uniq{|item ...| block}-> new_array

Argument(s) required:

This method does not take any argument. Instead, you will have to pass a block if you want to find unique objects based on certain criteria.

Example 1:

=begin
  Ruby program to demonstrate uniq method
=end

# array declaration
table = ["Subha","Sham","Sham","Vivek","Me","Amisha","Zain","Subha","Vivek"]

puts "Array uniq implementation"
new_arr = table.uniq

puts "Array after finding unique: #{new_arr}"
puts "Original Array instance: #{table}"

Output

Array uniq implementation
Array after finding unique: ["Subha", "Sham", "Vivek", "Me", "Amisha", "Zain"]
Original Array instance: ["Subha", "Sham", "Sham", "Vivek", "Me", "Amisha", "Zain", "Subha", "Vivek"]

Explanation:

In the above code, you can observe that we are finding unique elements in the Array instance with the help of the Array.uniq method. In the new Array, we have all the objects which are unique in the self Array. This method is not creating an impact upon the original Array instance because this method is one of the examples of non-destructive methods.

Example 2:

=begin
  Ruby program to demonstrate uniq method
=end

# array declaration
table = ["Subha","Sham","Sham","Vivek","Me","Amisha","Zain","Subha","Vivek"]

puts "Array uniq implementation"
new_arr = table.uniq

puts "Array after finding unique: #{new_arr}"
puts "Original Array instance: #{table}"

Output

Array uniq implementation
Array after finding unique: [["Subha", "Sham"], ["din", "dopher"]]
Original Array instance: [["Subha", "Sham"], ["Subha", "Raat"], ["din", "dopher"]]

Explanation:

In the above output, you can notice that we are calling the Array.uniq method with a block. Our self Array is a multidimensional Array that contains three sub Arrays. Though all the three Arrays are unique the method had carried out the comparison of the basis of the first object of all the three Array instances. This method is not creating an impact upon the original Array instance because this method is one of the examples of non-destructive methods.



Comments and Discussions!

Load comments ↻





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