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

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

Identify duplicates in an array

Method to return items that appear more than once in an array (or Enumerable)

module Enumerable
  def dups
    inject({}) {|h,v| h[v]=h[v].to_i+1; h}.reject{|k,v| v==1}.keys
  end
end


How it works: the inject method builds up a hash with keys for the unique values in the array and
a value representing the number of times the entry is found in the array. Any entries with a
value of 1 (i.e. not duplicated) are removed, and the resulting keys represents the duplicated
entries in the original array.

Example:

arr = %w{foo bar baz bar baz qux foo zub}
puts arr.dups.inspect
# => ["baz", "foo", "bar"]

"uniq"

you don't need an extra hash.
my @uniq = keys %{{ map { $_ => 1 } @list }};

'uniq' array

A much more idiomatic and efficient version of the 'uniq array' code at 152 is:

  # Input: @list
  # Output: @uniqed
  my %seen;
  my @uniqed = grep !$seen{$_}++, @list;


Removing duplicates from lists

A request for this came up on the CF-Talk list recently. Here's what I came up with.
<!--- The list with want to clean up --->
<cfset lst = "a,list,with,a,few,duplicates">
<!--- Use a struct to build a set of the list elements --->
<cfset set = StructNew()>
<cfloop index="elem" list="#lst#">
    <cfset set[elem] = "">
</cfloop>
<!--- Convert the set back to a list --->
<cfset lst = StructKeyList(set)>

This sort of this should be familiar to all you perlmongers out there.

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-5 of 5 total  RSS