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

问题描述 投票:-3回答:3

我有哈希数组:

[
  {: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"}重复的值

如何在控制台中找到副本:数字并显示如下输出:

=== 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
ruby hash
3个回答
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迭代您的数据。


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


Or
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

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"}
  ]
]
© www.soinside.com 2019 - 2024. All rights reserved.