Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Justin Mason http://jmason.org

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Efficient 'uniq' snippet

This snippet will quickly uniq an unsorted array; in other words, remove duplicates. (note: for large arrays, it may be more efficient to sort the array first and simply keep track of consecutive dups instead of using a hash.)

  # Input: @list
  # Output: @uniqed
  my %u = ();
  @uniqed = grep {defined} map {
      if (exists $u{$_}) { undef; } else { $u{$_}=undef;$_; }
    } @list;
  undef %u;


« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS