<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: duplicates code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 17 May 2008 13:52:51 GMT</pubDate>
    <description>DZone Snippets: duplicates code</description>
    <item>
      <title>Identify duplicates in an array</title>
      <link>http://snippets.dzone.com/posts/show/3838</link>
      <description>Method to return items that appear more than once in an array (or Enumerable)&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;module Enumerable&lt;br /&gt;  def dups&lt;br /&gt;    inject({}) {|h,v| h[v]=h[v].to_i+1; h}.reject{|k,v| v==1}.keys&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;How it works: the inject method builds up a hash with keys for the unique values in the array and&lt;br /&gt;a value representing the number of times the entry is found in the array. Any entries with a&lt;br /&gt;value of 1 (i.e. not duplicated) are removed, and the resulting keys represents the duplicated&lt;br /&gt;entries in the original array.&lt;br /&gt;&lt;br /&gt;Example:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;arr = %w{foo bar baz bar baz qux foo zub}&lt;br /&gt;puts arr.dups.inspect&lt;br /&gt;# =&gt; ["baz", "foo", "bar"]&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Thu, 19 Apr 2007 00:21:22 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3838</guid>
      <author>bshow (Bob Showalter)</author>
    </item>
    <item>
      <title>"uniq"</title>
      <link>http://snippets.dzone.com/posts/show/366</link>
      <description>you don't need an extra hash.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;my @uniq = keys %{{ map { $_ =&gt; 1 } @list }};&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 04 Jun 2005 14:56:16 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/366</guid>
      <author>pasang_lhamu (Pasang Lhamu Sherpa)</author>
    </item>
    <item>
      <title>'uniq' array</title>
      <link>http://snippets.dzone.com/posts/show/354</link>
      <description>A much more idiomatic and efficient version of the 'uniq array' code at &lt;a href="http://www.bigbold.com/snippets/posts/show/152 "&gt;152&lt;/a&gt; is:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  # Input: @list&lt;br /&gt;  # Output: @uniqed&lt;br /&gt;  my %seen;&lt;br /&gt;  my @uniqed = grep !$seen{$_}++, @list;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;</description>
      <pubDate>Mon, 30 May 2005 21:51:55 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/354</guid>
      <author>TMtm (Tony Bowden)</author>
    </item>
    <item>
      <title>Removing duplicates from lists</title>
      <link>http://snippets.dzone.com/posts/show/206</link>
      <description>A request for this came up on the CF-Talk list recently. Here's what I came up with.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;!--- The list with want to clean up ---&gt;&lt;br /&gt;&lt;cfset lst = "a,list,with,a,few,duplicates"&gt;&lt;br /&gt;&lt;!--- Use a struct to build a set of the list elements ---&gt;&lt;br /&gt;&lt;cfset set = StructNew()&gt;&lt;br /&gt;&lt;cfloop index="elem" list="#lst#"&gt;&lt;br /&gt;    &lt;cfset set[elem] = ""&gt;&lt;br /&gt;&lt;/cfloop&gt;&lt;br /&gt;&lt;!--- Convert the set back to a list ---&gt;&lt;br /&gt;&lt;cfset lst = StructKeyList(set)&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;This sort of this should be familiar to all you perlmongers out there.</description>
      <pubDate>Thu, 21 Apr 2005 22:09:23 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/206</guid>
      <author>keith (Keith Gaughan)</author>
    </item>
    <item>
      <title>Efficient 'uniq' snippet</title>
      <link>http://snippets.dzone.com/posts/show/152</link>
      <description>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.)&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  # Input: @list&lt;br /&gt;  # Output: @uniqed&lt;br /&gt;  my %u = ();&lt;br /&gt;  @uniqed = grep {defined} map {&lt;br /&gt;      if (exists $u{$_}) { undef; } else { $u{$_}=undef;$_; }&lt;br /&gt;    } @list;&lt;br /&gt;  undef %u;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;</description>
      <pubDate>Wed, 13 Apr 2005 03:26:57 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/152</guid>
      <author>jmason (Justin Mason)</author>
    </item>
  </channel>
</rss>
