Open In App

Ruby | Array uniq!() function

Last Updated : 06 Dec, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

Array#uniq!() : uniq!() is a Array class method which removes duplicate elements from the array and returns only if there is duplicate element in the array.

Syntax: Array.uniq!()

Parameter: Array

Return: removes duplicate elements from the array.

Example #1 :




# Ruby code for uniq!() method
  
# declaring array
a = [18, 22, 33, nil, 5, 6]
  
# declaring array
b = [1, 4, 1, 1, 88, 9]
  
# declaring array
c = [18, 22, 50, 6]
  
# uniq! method example
puts "uniq!() method form : #{a.uniq!()}\n\n"
  
puts "uniq!() method form : #{b.uniq!()}\n\n"
  
puts "uniq!() method form : #{c.uniq!()}\n\n"


Output :

uniq!() method form : 

uniq!() method form : [1, 4, 88, 9]

uniq!() method form : 

Example #2 :




# Ruby code for uniq!() method
  
# declaring array
a = ["abc", "nil", "dog"]
  
# declaring array
c = ["cat", nil, "cat"]
  
# declaring array
b = ["cow", nil, "dog"]
  
  
# uniq! method example
puts "uniq!() method form : #{a.uniq!()}\n\n"
  
puts "uniq!() method form : #{b.uniq!()}\n\n"
  
puts "uniq!() method form : #{c.uniq!()}\n\n"


Output :

uniq!() method form : 

uniq!() method form : 

uniq!() method form : ["cat", nil]



Similar Reads

Ruby | Array uniq() function
Array#uniq() : uniq() is a Array class method which returns a new array by removing duplicate values in the array. Syntax: Array.uniq() Parameter: Array Return: a new array by removing duplicate values in the array Example #1 : # Ruby code for uniq() method # declaring array a = [18, 22, 33, nil, 5, 6] # declaring array b = [1, 4, 1, 1, 88, 9] # de
2 min read
Ruby | Enumerator::Lazy#uniq function
The uniq function in Ruby is used to find the unique set of elements from the given array. Syntax: xs.uniq Here, xs is a array of elements. Parameters: This function does not accept any parameters. Returns: the new set of unique values. Example 1: # Initialising an array with blocks as elements xs = [ [1, "GFG", 10], [1, "GFG",
1 min read
Ruby | Enumerable uniq() function
The uniq() of enumerable is an inbuilt method in Ruby returns an array removing all the duplicates in the given enum. Syntax: enu.uniq Parameters: The function accepts no parameter. Return Value: It returns an array. Example 1: # Ruby program for uniq method in Enumerable # Initialize enu = [1, 1, 2, 2, 4, 4] # Prints enu.uniq Output: [1, 2, 4] Exa
1 min read
Difference between Ruby and Ruby on Rails
1. Ruby : Ruby is an object-oriented scripting language launched in 1995 and is known as a general-purpose programming language. It was programmed in C programming language. Ruby is a secured programming language and its syntax is similar to Perl and Python. It was developed on the principle of user interface design and it is mainly used to develop
3 min read
Ruby | Array Concatenation using (+) function
Array#+() is a Array class method which performs set concatenate operation arrays by combining two arrays to a third array. Syntax: Array.+() Parameter: Arrays for performing the concatenation operation. Return: New arrays by combining two arrays. Example #1 : # Ruby code for +() method # showing concatenate operation # declaring array a = [18, 22,
2 min read
Ruby | Array assoc() function
The assoc() function in Ruby is used to search through an array of arrays whose first element is compared with the index of the function and return the contained array if match found otherwise return either nil or vacant. Syntax: Array.assoc(Object) Here Array is the array of arrays. Parameters: Object : It is an element which gets compared with th
2 min read
Ruby | Array abbrev() function
abbrev() is an Array class method which provides unambiguous set of abbreviations for the string. Syntax: Array.abbrev()Parameter:- string for abbreviation - pattern [optional]Return: unambiguous abbreviations set Example #1 : Example for abbrev() method C/C++ Code # Ruby code for abbrev() method # checking for unambiguous abbreviations require 'ab
1 min read
Ruby Array () function with example
Array#() : () is an Array class method which performs the comparison between the two arrays. Syntax: Array.() Parameter: Array for the comparison Return: -1 : array is less than the other array 0 : array is equal to other array 1 : array is greater than the other array Example #1 : # Ruby code for <=>() method # showing comparison # declaring
2 min read
Ruby | Array class first() function
first() is a Array class method which returns the first element of the array or the first 'n' elements from the array. Syntax: Array.first() Parameter: Array n - no. of elements Return: first element of the array or the first 'n' elements from the array Example #1 : # Ruby code for first() method # declaring array a = [18, 22, 33, nil, 5, 6] # decl
1 min read
Ruby | Array class flatten() function
flatten() is an Array class method which returns flattened array i.e. a 1D array Syntax: Array.flatten() Parameter: Array Return: 1D array Example #1 : # Ruby code for flatten() method # declaring array a = [[18, 22], [ 33, nil, 5, 6]] # declaring array b = [[[1, 4, 1, 1, 88, 9]]] # flatten puts "flatten : #{a.flatten()}\n\n" # flatten pu
1 min read