in reply to What is the best way to get just the duplicates from an array?

This is a FAQ. Please see perldoc -q duplicate or perlfaq4, How can I remove duplicate elements from a list or array. Obviously you want to keep just the duplicates instead of removing them, but doing so is left as an exercise.

  • Comment on Re: What is the best way to get just the duplicates from an array?
  • Download Code

Replies are listed 'Best First'.
Re^2: What is the best way to get just the duplicates from an array?
by wenD (Beadle) on Jul 27, 2007 at 15:56 UTC

    Thanks! The perlfaq4 example (with the ! for unique removed) is the one that I'll use from now on.

    my %seen = (); my @duplicates = grep { $seen{ $_ }++ } @array;
      That won't work. If there's three of the same thing, it will print the duplicate twice. That's why I used == in my earlier post instead of (an implied) >.