Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

(jeffa) Re: Removing Duplicates in an array?

by jeffa (Bishop)
on Jun 26, 2001 at 16:58 UTC ( [id://91574]=note: print w/replies, xml ) Need Help??


in reply to Removing Duplicates in an array?

Super Search is you friend! I did a super search for 'remove duplicate' in the body text and found How can I extract just the unique elements of an array?.

Jeff

R-R-R--R-R-R--R-R-R--R-R-R--R-R-R--
L-L--L-L--L-L--L-L--L-L--L-L--L-L--
  • Comment on (jeffa) Re: Removing Duplicates in an array?

Replies are listed 'Best First'.
Re: (jeffa) Re: Removing Duplicates in an array?
by Tiefling (Monk) on Jun 26, 2001 at 17:20 UTC
    Is it just me, or does the FAQ title 'How can I extract just the unique elements of an array?' not actually convey what the solution does accurately?

    The unique elements of ("fish","bread","eggs","eggs","fish","butter") are ("bread","butter").

    And as a quick golf question: how would you extract the genuinely unique elements of a list?

    Tiefling

    -----BEGIN GEEK CODE BLOCK----- Version: 3.1 GAT d++ s:- a-- C++ UL P++ L++(+) E? W+(++) N+ o? K w+(--) !O M- V? PS+ PE- Y PGP- t+ 5 X+ R+++ tv- b+++ DI++++ D+ G+ e++ h!(-) y +? ------END GEEK CODE BLOCK------
      At 2818 chars, I golf:
      sub u{@h{@_}=@_;keys%h} print join "\t", u("fish","bread","eggs","eggs","fish","butter") #butter bread fish eggs
      /me wonders how soon MeowChow will have a shorter solution...

      Jeroen
      "We are not alone"(FZ)
      Update: Too bad. The shortest I can come with is just a rewrite of Davorg's code (he's a golfer in disguise):

      sub u{$h{$_}++for@_;grep$h{$_}<2,keys%h}
        Nice golf, jeroenes. Trouble is, (if your sample output is right) it answers the original question, not the one I posed as a golf. What's the shortest way to produce exactly the elements which occurs uniquely (ie: only once) in the original array? Davorg's code does the right thing, but it's a canonical answer, as he admits, rather than golf.

        Tiefling (Just Another Perl Newbie)

        -----BEGIN GEEK CODE BLOCK----- Version: 3.1 GAT d++ s:- a-- C++ UL P++ L++(+) E? W+(++) N+ o? K w+(--) !O M- V? PS+ PE- Y PGP- t+ 5 X+ R+++ tv- b+++ DI++++ D+ G+ e++ h!(-) y +? ------END GEEK CODE BLOCK------
        Well, I've already golfed it down to 30 chars, but I thought you might enjoy this rather bizarre one at 33:
        sub u { delete@h{grep$h{$_}++,@_};keys%h }
        As with yours, my%h; should really be prepended to make the sub reusable.
           MeowChow                                   
                       s aamecha.s a..a\u$&owag.print
      My shortest is 30 chars:
      sub u { grep{$a=$_;2>grep$_ eq$a,@_}@_ }
         MeowChow                                   
                     s aamecha.s a..a\u$&owag.print

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://91574]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2024-04-24 21:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found