Remove duplicates from loop

后端 未结 3 1129
醉话见心
醉话见心 2021-01-26 00:57

i have following code.

for i in 0..sold.length-1
    duplicate = sold[i]
    print duplicate.check_duplicates
    print \"     \"
    print sold[i].lotnumber + \         


        
相关标签:
3条回答
  • 2021-01-26 01:17
    uniq_solds = sold.collect{|s| [s.check_duplicates, s.lotnumber, s.serialnumber]}.uniq
    
    uniq_solds.each do |s|
      p s.join("      ")
    end
    
    0 讨论(0)
  • 2021-01-26 01:20

    You can try using something called uniq!, here is a link to the API.

    http://www.ruby-doc.org/core-2.0.0/Array.html#method-i-uniq-21


    0 讨论(0)
  • 2021-01-26 01:29

    The hash resulting from the group_by method can be used as a counting device; this does not use the check_duplicates method.

    grouped = sold.group_by{|item| [item.lotnumber, item.serialnumber]}
    grouped.each{|key, value| puts "#{value.size}   #{key.first}\t#{key.last}"}
    
    0 讨论(0)
提交回复
热议问题