ruby remove duplicates from multidimensional array

来源:互联网 发布:4399刷盒币软件 编辑:程序博客网 时间:2024/05/17 12:04

names = ["Bob", "Dave", "Clive"]names2 = names.repeated_combination(2).to_a[["Bob", "Bob"], ["Bob", "Dave"], ["Bob", "Clive"], ["Dave", "Dave"], ["Dave", "Clive"],     ["Clive", "Clive"]]

I would do :

names = ["Bob", "Dave", "Clive"]names.combination(2).to_a# => [["Bob", "Dave"], ["Bob", "Clive"], ["Dave", "Clive"]]

and to correct your code :-

non_uniq_comb = names.repeated_combination(2).reject { |a,b| a == b }

0 0
原创粉丝点击