如何找到哈希的重复数组并在Ruby中打印它们

时间:2019-02-21 08:21:15

标签: ruby hash

我有哈希数组:

[
  {:name=>"King Summer", :number=>"0034242342"}, 
  {:name=>"Max Snow", :number=>"899899080"}, 
  {:name=>"Duck Doe", :number=>"90897688"}, 
  {:name=>"Shark Jon", :number=>"0034242342"}
]

在上面,键:number {:name=>"King Summer", :number=>"0034242342"}的值与{:name=>"Shark Jon", :number=>"0034242342"}重复

如何找到重复的:number 并在控制台中显示如下输出:

=== List with duplicate number ===
King Summer – 0034242342 : ok
Max Snow – 899899080 : ok
Duck Doe – 90897688 : ok
Shark Jon – 0034242342 : duplicate number

== List without duplicate number ===
1 - King Summer – 0034242342
2 - Max Snow – 899899080
3 - Duck Doe – 90897688

3 个答案:

答案 0 :(得分:2)

您可以简单地使用inject添加状态,其中inject的累加器将保存先前迭代中传递的数字,

arr.inject([]) { |m,a| m.include?(a[:number]) ? (a[:status] = 'duplicated number') : ((m << a[:number]) && (a[:status] = 'ok')); m} 

arr
# [
#  {:name=>"King Summer", :number=>"0034242342", :status=>"ok"}, 
#  {:name=>"Max Snow", :number=>"899899080", :status=>"ok"}, 
#  {:name=>"Duck Doe", :number=>"90897688", :status=>"ok"}, 
#  {:name=>"Shark Jon", :number=>"0034242342", :status=>"duplicated number"}
# ]

稍后使用each迭代此数据。

答案 1 :(得分:0)

您可以这样做。也许不是最干净的方法。

duplicated = []
non_duplicated = []

array = [
  {:name=>"King Summer", :number=>"0034242342"}, 
  {:name=>"Max Snow", :number=>"899899080"}, 
  {:name=>"Duck Doe", :number=>"90897688"}, 
  {:name=>"Shark Jon", :number=>"0034242342"}
]

array.each do |a| 

  is_duplicated = non_duplicated.find do |i|
    i == a[:number]
  end

  if is_duplicated
    duplicated.push(a[:number])
    non_duplicated.delete(a[:number])
    next
  end

  non_duplicated.push(a[:number])

end

编辑

替代

array.partition do |value|
  number = value[:number]

  array.map do |i|
    i[:number]
  end.count(number) > 1
end

这将返回一个包含2个部分的数组(重复的和其余的):

[
  [
    {:name=>"King Summer", :number=>"0034242342"}, 
    {:name=>"Shark Jon", :number=>"0034242342"}
  ], 
  [
    {:name=>"Max Snow", :number=>"899899080"},
    {:name=>"Duck Doe", :number=>"90897688"}
  ]
]

答案 2 :(得分:0)

其他选择是建立一个散列,该散列将重复的(true)而不是(false)分组。

给出数组:

ary = [
        {name: "King Summer", number: "0034242342"}, 
        {name: "Max Snow", number: "899899080"}, 
        {name: "Duck Doe", number: "90897688"}, 
        {name: "Shark Jon", number: "0034242342"},
        {name: "Jim Kirk", number: "90897688"},
        {name: "Mr. Spock", number: "10897688"}
      ]

构建哈希:

res = ary.group_by { |h| h[:number] }.group_by { |_,v| v.size > 1 }.transform_values(&:to_h)

#=> {true=>{"0034242342"=>[{:name=>"King Summer", :number=>"0034242342"}, {:name=>"Shark Jon", :number=>"0034242342"}], "90897688"=>[{:name=>"Duck Doe", :number=>"90897688"}, {:name=>"Jim Kirk", :number=>"90897688"}]}, false=>{"899899080"=>[{:name=>"Max Snow", :number=>"899899080"}], "10897688"=>[{:name=>"Mr. Spock", :number=>"10897688"}]}}

然后列出重复项:

res[true].each do |number, people|
  puts "- ph. number: #{number}"
  people.each { |person| puts "\t"*2 + person[:name] }
end

# - ph. number: 0034242342
#     King Summer
#     Shark Jon
# - ph. number: 90897688
#     Duck Doe
#     Jim Kirk


res.each do |_, list|
  puts "-"*10
  list.each do |number, people|
    puts "- ph. number: #{number}"
    people.each { |person| puts "\t"*2 + person[:name] }
  end
end

# ----------
# - ph. number: 0034242342
#     King Summer
#     Shark Jon
# - ph. number: 90897688
#     Duck Doe
#     Jim Kirk
# ----------
# - ph. number: 899899080
#     Max Snow
# - ph. number: 10897688
#     Mr. Spock