Ruby | Array &() function
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 :
# Ruby code for &() method # declaring arrays a = [ 18 , 22 , 33 , 4 , 5 , 6 ] # declaring arrays b = [ 18 , 22 , 33 , 4 , 5 , 6 ] # declaring arrays c = [ 18 , 22 , 33 , 40 , 50 , 6 ] # & method puts "& method form : #{a & b}\n\n" # & method puts "& method form : #{a & c}\n\n" # & method puts "& method form : #{b & c}\n\n" |
chevron_right
filter_none
Output :
& method form : [18, 22, 33, 4, 5, 6] & method form : [18, 22, 33, 6] & method form : [18, 22, 33, 6]
Example #2 :
# Ruby code for &() method # declaring arrays a = [ "abc" , "xyz" , "dog" ] # declaring arrays b = [ "cat" , "cat" , "dog" ] # declaring arrays c = [ "cat" , "cat" , "dog" ] # & method puts "& method form : #{a & b}\n\n" # & method puts "& method form : #{a & c}\n\n" # & method puts "& method form : #{b & c}\n\n" |
chevron_right
filter_none
Output :
& method form : ["dog"] & method form : ["dog"] & method form : ["cat", "dog"]
Recommended Posts:
- Ruby | Array none?() function
- Ruby | Array take() function
- Ruby | Array map() function
- Ruby Array () function with example
- Ruby | Array <=> function
- Ruby | Array max() function
- Ruby | Array one?() function
- Ruby | Array zip() function
- Ruby | Array min() function
- Ruby | Array pop() function
- Ruby | Array rotate!() function
- Ruby | Array sort() function
- Ruby | Array rotate() function
- Ruby | Array abbrev() function
- Ruby | Array sample() function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.