Home »
Ruby Tutorial
Removing elements from the Array using of Array.compact and Array.uniq methods in Ruby
By IncludeHelp Last updated : November 22, 2024
In the last article, we have gone through two different methods of deleting elements from the Array. We have seen their implementation with the help of their syntaxes and supporting examples. For a quick recall, let me tell those two methods were Array.delete() and Array.delete_at().
The Array.delete() is a method that requires the name of the element which is supposed to be passed in the argument list of the Array. It works in a way that it deletes all the elements of the same name as mentioned in the argument list of Array.delete() whereas Array.delete_at() requires the index of the element we want to remove from the Array.
In the article, we will learn about two more and important methods of deleting the elements from the instance of Array class namely Array.comp() and Array.uniq()!.
Now let us go through their implementation with the help of their syntaxes and examples which you will find in the rest of the article.
Using Array.compact Method
In most of the cases, you will find that your array is containing some nil values and you will be requiring some mechanism or lines of code to delete those nil values which are present in your Array. Ruby decreases your overhead of writing code for the same purpose by providing you a method named Array.compact. This method is used to remove all the nil values from the object of the Array class. Array.compact! is used to make changes in the Array as well.
This method requires no arguments.
Syntax
Array.compact
# or
Array.compact!
Example
=begin
Ruby program to remove elements from Array
using Array.compact
=end
# array declaration
Adc = [nil,'Includehelp.com','Ruby','C++','C#','Java','Python','C++',nil,nil]
# Array.compact method
puts "Array.compact!"
Adc.compact
# printing array elements
puts "Array elements are:"
print Adc
puts ""
# Array.compact! method
puts "Array.compact!"
Adc.compact!
# printing array elements
puts "Array elements are:"
print Adc
Output
Array.compact!
Array elements are:
[nil, "Includehelp.com", "Ruby", "C++", "C#", "Java", "Python", "C++", nil, nil]
Array.compact!
Array elements are:
["Includehelp.com", "Ruby", "C++", "C#", "Java", "Python", "C++"]
Explanation
In the above code, you can observe that Array.compact can be used to remove nil elements from the Array whereas Array.compact! can be used to make changes in the same Array as well.
Using Array.uniq Method
Array.uniq or Array.uniq! can be used to remove duplicate elements from the Array. Sometimes you may observe that your Array is containing duplicate entries which may create any kind of complexity. Array.uniq is non-destructive whereas Array.uniq! is destructive, which means that the latter will create changes in the same Array as well.
No arguments required.
Syntax
Array.uniq
# or
Array.uniq!
Example
=begin
Ruby program to remove elements from Array
using Array.uniq
=end
# array declaration
Adc = ['Ruby','Includehelp.com','Ruby','C++','C#','Java','Python','C++','Java']
# Array.uniq Method
puts "Array.uniq"
print Adc.uniq
puts ""
puts "Array elements are:"
print Adc
puts ""
# Array.uniq! Method
puts "Array.uniq!"
Adc.uniq!
puts "Array elements are:"
print Adc
Output
Array.uniq
["Ruby", "Includehelp.com", "C++", "C#", "Java", "Python"]
Array elements are:
["Ruby", "Includehelp.com", "Ruby", "C++", "C#", "Java", "Python", "C++", "Java"]
Array.uniq!
Array elements are:
["Ruby", "Includehelp.com", "C++", "C#", "Java", "Python"]
Explanation
In the above code, you can see that Array.uniq is not creating any change in the Array whereas it is possible with the help of Array.uniq! method. You can go for Sets if you want to avoid duplicacy.