Afficher un message
Vieux 16/09/2007, 13h13   #3
Ruhe
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Search for duplicate elements in the array

On 16 , 15:32, Robert Klemme <shortcut...@googlemail.com> wrote:
> This is way inefficient. You rather want a counter per element stored
> in a Map (HashMap or TreeMap in Java).
>
> dups = Hash.new 0
> arr.each {|x| dups[x]+=1}
> puts dups.select {|k,v| v>1}
>
> You can have it on one line if you prefer:
>
> puts arr.inject(Hash.new(0)) {|d,x| d[x]+=1; d}.select {|k,v| v>1}
>
> Kind regards
>
> robert


Thanks a lot! This is a new level of programing knowledge for me.
I'll try to make something like this with HashMap in Java.

  Réponse avec citation
 
Page generated in 0,04429 seconds with 9 queries