From: Nico Landgraf Date: 2006-10-28T03:00:17+09:00 Subject: Re: how to get only the duplicatet items from an array thanks paul, looks like my way. i hoped there is a smarter way in ruby. Paul Lutus wrote: > Nico Landgraf wrote: > >> i have an array like this >> aTemp = [1, 1, 2, 2, 3, 4] >> and i want to know, which items are duplicatet. > > / ... > >> so i found an bad solution and hope you can help me to get the right way. > > #!/usr/bin/ruby -w > > a = [1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 4, 5, 6, 7, 8, 9, 9, 9] > > dups = [] > > hash = {} > > a.each do |item| > dups << item if hash[item] > hash[item] = true > end > > p dups > > [1, 1, 1, 1, 2, 2, 2, 9, 9] >