Ruby | Array &() function
Last Updated :
05 Dec, 2019
Array#&() : &() is an Array class method which returns a new array containing unique elements common to the two arrays.
Syntax: Array.&()
Parameter: Array for the comparison
Return: a new array containing unique elements common to the two arrays
Example #1 :
a = [ 18 , 22 , 33 , 4 , 5 , 6 ]
b = [ 18 , 22 , 33 , 4 , 5 , 6 ]
c = [ 18 , 22 , 33 , 40 , 50 , 6 ]
puts "& method form : #{a & b}\n\n"
puts "& method form : #{a & c}\n\n"
puts "& method form : #{b & c}\n\n"
|
Output :
& method form : [18, 22, 33, 4, 5, 6]
& method form : [18, 22, 33, 6]
& method form : [18, 22, 33, 6]
Example #2 :
a = [ "abc" , "xyz" , "dog" ]
b = [ "cat" , "cat" , "dog" ]
c = [ "cat" , "cat" , "dog" ]
puts "& method form : #{a & b}\n\n"
puts "& method form : #{a & c}\n\n"
puts "& method form : #{b & c}\n\n"
|
Output :
& method form : ["dog"]
& method form : ["dog"]
& method form : ["cat", "dog"]
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...