Aayush
Aayush

Reputation: 1254

Deleting duplicate data in ruby

I want to remove duplicates from my code based on all the three fields i.e based on event-name,date and time,if all the three fields match for two events then one should be removed showing only unique events.The code I have written works for only for one field i.e its removing entries based on event-name.How do i match all the three fields. I was using & but that didn't work out.

include Enumerable

list = [
  {
     :eventname => "music show",
     :date => "1st august",
     :time => "9 pm"
  },
  {
      :eventname => "dance show",
      :date => "11st august",
      :time => "9 pm"
  },
  {
      :eventname => "music show",
      :date => "1st august",
      :time => "9 pm"
  },
  {
       :eventname => "music show",
       :date => "15st august",
       :time => "9 pm"
  },
  {
      :eventname => "magic show",
      :date => "12st august",
      :time => "9 pm"
  },
  {
      :eventname => "rock show",
      :date => "1st august",
      :time => "9 pm"
  }
]

b=list.group_by{|r| r[:eventname]}.map do |k, v|
    v.inject({}) { |r, h| r.merge(h){ |key, o, n| o || n } }
end

Upvotes: 0

Views: 74

Answers (1)

Yossi
Yossi

Reputation: 12090

Use the uniq mthod of Array:

list.uniq

Upvotes: 4

Related Questions