I have two arrays that have thousands of elements. I need to find what elements are missing in one array by comparing it to another. Is there a way to get the missing elements without iterating through the entire array? Or is there something faster than what I am doing?
Here is what I am using now:
def find_missing(array1, array2)
missing_elements = []
array1.each { |e|
unless array2.include? e
missing_elements << e
end
}
return missing_elements
end
array1 = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
array2 = [1, 2, 4, 5, 6, 7, 9]
puts find_missing(array1, array2)