I have this function:
def find_deleted_number(arr, mixed_arr)
default = 0
arr.each do |i|
if mixed_arr.include?(arr[i]) == true
default += arr[i]
else
return arr[i]
end
end
return default
end
find_deleted_number([1,2,3,4,5], [3,4,1,5])
#=> 2 (as expected)
find_deleted_number([1,2,3,4,5,6,7,8,9], [1,9,7,4,6,2,3,8])
#=> 5 (as expected)
find_deleted_number([1,2,3,4,5,6,7,8,9], [5,7,6,9,4,8,1,2,3])
#=> nil (expected 45)
The idea is for the function to check which number is in arr but not in mixed_arr. That's all good but the problem is that when both arrays are the same, I want it to give me the sum of every value in one of the arrays (that's why I do default += arr[i]). But the result is nil and I dont quite understand why. If possible I would like to understand the reason why it is failing.
sumfind_deleted_number([1,2,3,4,5,6,7,8,9,9], [5,7,6,9,4,8,1,2,3])? Note the repeated9. Of course if this case was a possible input.