Ruby array subtraction without removing items more than once

后端 未结 4 1074
余生分开走
余生分开走 2020-12-03 16:55

The canonical Array difference example in Ruby is:

[ 1, 1, 2, 2, 3, 3, 4, 5 ] - [ 1, 2, 4 ]  #=>  [ 3, 3, 5 ]

What\'s the best way to ge

4条回答
  •  醉梦人生
    2020-12-03 17:34

    This is all I can think of so far:

    [1, 2, 4].each { |x| ary.delete_at ary.index(x) }
    

提交回复
热议问题