programming-examples/ruby/Algorithms/difference_between_arrays.rb

10 lines
305 B
Ruby
Raw Normal View History

2019-11-15 12:59:38 +01:00
# Write a function (similar to _.difference) that takes two arrays
# and returns two arrays containing the elements not present in the other
def difference(arr1, arr2)
result_1 = arr1.reject { |num| arr2.include?(num) }
result_2 = arr2.reject { |num| arr1.include?(num) }
[result_1, result_2]
end