Solved

Duplicates

Posted on 1998-09-29
1
189 Views
Last Modified: 2010-03-05
What is the most efficient way to strip duplicates from an array?
0
Comment
Question by:IsleOfView
1 Comment
 
LVL 5

Accepted Solution

by:
b2pi earned 40 total points
ID: 1205023
perldoc perlfaq4

     a) If @in is sorted, and you want @out to be sorted:
 
             $prev = 'nonesuch';
             @out = grep($_ ne $prev && ($prev = $_), @in);
 
         This is nice in that it doesn't use much extra memory,
         simulating uniq(1)'s behavior of removing only adjacent
         duplicates.
 
     b) If you don't know whether @in is sorted:
 
             undef %saw;
             @out = grep(!$saw{$_}++, @in);
 
     c) Like (b), but @in contains only small integers:
 
             @out = grep(!$saw[$_]++, @in);
 
     d) A way to do (b) without any loops or greps:
 
             undef %saw;
             @saw{@in} = ();
             @out = sort keys %saw;  # remove sort if undesired
 

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now