Ruby | Array Difference (-) function
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 :
# Ruby code for -() method# showing concatenate operation # declaring arraya = [18, 22, 33, 4, 5, 6] # declaring arrayb = [5, 4, 22, 1, 88, 9] # declaring arrayc = [18, 22, 33, 40, 50, 6] # differencing arraysputs "difference of a and b : #{a - b}\n\n" # differencing arraysputs "difference of a and c : #{a - c}\n\n" # differencing arraysputs "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 :
# Ruby code for -() method# showing concatenate operation # declaring arraya = ["abc", "xyz", "dog"] # declaring arrayb = ["cow", "cat", "dog"] # declaring arrayc = ["cat", "1", "dog"] # differencing arraysputs "difference of a and b : #{a - b}\n\n" # differencing arraysputs "difference of a and c : #{a - c}\n\n" # differencing arraysputs "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"]
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.


