Find number deleted from array

前端 未结 2 1957
攒了一身酷
攒了一身酷 2021-01-25 06:35

I am quite new to ruby so bear with me, I have two arrays and we are meant to find the number which is missing.

The starting array sequence is [1,2,3,4,5,6,7,8,9] The mi

相关标签:
2条回答
  • 2021-01-25 06:39

    Use Array Difference

    [1,2,3,4,5,6,7,8,9] - [3,2,4,6,7,8,1,9]
     #=> [5] 
    

    From the docs:

    ary - other_ary → new_ary

    Returns a new array that is a copy of the original array, removing any items that also appear in other_ary. The order is preserved from the original array.

    It compares elements using their hash and eql? methods for efficiency

    0 讨论(0)
  • 2021-01-25 06:44

    You are passing in mixed as an argument and then are using it as mixed_arr.inject..., seems to work fine making those consistent.

    Also, since you are passing in the arr and mixed you don't need them to be set in the method, you can call it as

    find_deleted_number([1,2,3,4,5,6,7,8,9], [3,2,4,6,7,8,1,9])
    

    and then remove

    arr = [1,2,3,4,5,6,7,8,9]
    mixed = [3,2,4,6,7,8,1,9]
    

    from the beginning of your method.

    0 讨论(0)
提交回复
热议问题