Array#-() is a Array class method which performs set difference operation by removing the similar elements of the two array.
Syntax: Array.-()
Parameter: Arrays for performing the concatenation operation.
Return: New arrays by removing the same elements of the two arrays.
Example #1 :
a = [ 18 , 22 , 33 , 4 , 5 , 6 ]
b = [ 5 , 4 , 22 , 1 , 88 , 9 ]
c = [ 18 , 22 , 33 , 40 , 50 , 6 ]
puts "difference of a and b : #{a - b}\n\n"
puts "difference of a and c : #{a - c}\n\n"
puts "difference of b and c : #{b - c}\n\n"
|
Output :
difference of a and b : [18, 33, 6]
difference of a and c : [4, 5]
difference of b and c : [5, 4, 1, 88, 9]
Example #2 :
a = [ "abc" , "xyz" , "dog" ]
b = [ "cow" , "cat" , "dog" ]
c = [ "cat" , "1" , "dog" ]
puts "difference of a and b : #{a - b}\n\n"
puts "difference of a and c : #{a - c}\n\n"
puts "difference of b and c : #{b - c}\n\n"
|
Output :
difference of a and b : ["abc", "xyz"]
difference of a and c : ["abc", "xyz"]
difference of b and c : ["cow"]
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
07 Jan, 2020
Like Article
Save Article