<p>I have an array in Perl:</p>

<pre><code>my @my_array = ("one","two","three","two","three");
</code></pre>

<p>How do I remove the duplicates from the array?</p>

[{'answer_id': 7657, 'author': 'Greg Hewgill', 'author_id': 893, 'author_profile': 'https://Stackoverflow.com/users/893', 'pm_score': 8, 'selected': True, 'text': '<p>You can do something like this as demonstrated in <a href="http://perldoc.perl.org/perlfaq4.html#How-can-I-remove-duplicate-elements-from-a-list-or-array%3f" rel="noreferrer">perlfaq4</a>:</p>\n\n<pre><code>sub uniq {\n    my %seen;\n    grep !$seen{$_}++, @_;\n}\n\nmy @array = qw(one two three two three);\nmy @filtered = uniq(@array);\n\nprint "@filtered\\n";\n</code></pre>\n\n<p>Outputs:</p>\n\n<pre><code>one two three\n</code></pre>\n\n<p>If you want to use a module, try the <code>uniq</code> function from <a href="https://metacpan.org/pod/List::MoreUtils" rel="noreferrer"><code>List::MoreUtils</code></a></p>\n'}, {'answer_id': 7658, 'author': 'Xetius', 'author_id': 274, 'author_profile': 'https://Stackoverflow.com/users/274', 'pm_score': 5, 'selected': False, 'text': '<p>My usual way of doing this is:</p>\n\n<pre><code>my %unique = ();\nforeach my $item (@myarray)\n{\n    $unique{$item} ++;\n}\nmy @myuniquearray = keys %unique;\n</code></pre>\n\n<p>If you use a hash and add the items to the hash. You also have the bonus of knowing how many times each item appears in the list.</p>\n'}, {'answer_id': 7829, 'author': 'John Siracusa', 'author_id': 164, 'author_profile': 'https://Stackoverflow.com/users/164', 'pm_score': 7, 'selected': False, 'text': "<p>The Perl documentation comes with a nice collection of FAQs.  Your question is frequently asked:</p>\n<pre><code>% perldoc -q duplicate\n</code></pre>\n<p>The answer, copy and pasted from the output of the command above, appears below:</p>\n<hr>\nFound in /usr/local/lib/perl5/5.10.0/pods/perlfaq4.pod\n<p><strong>How can I remove duplicate elements from a list or array?</strong>\n(contributed by brian d foy)</p>\n<p>Use a hash. When you think the words &quot;unique&quot; or &quot;duplicated&quot;, think\n&quot;hash keys&quot;.</p>\n<p>If you don't care about the order of the elements, you could just create the hash then extract the keys. It's not important how you create that hash: just that you use &quot;keys&quot; to get the unique elements.</p>\n<pre><code>   my %hash   = map { $_, 1 } @array;\n   # or a hash slice: @hash{ @array } = ();\n   # or a foreach: $hash{$_} = 1 foreach ( @array );\n\n   my @unique = keys %hash;\n</code></pre>\n<p>If you want to use a module, try the &quot;uniq&quot; function from\n&quot;List::MoreUtils&quot;. In list context it returns the unique elements, preserving their order in the list. In scalar context, it returns the number of unique elements.</p>\n<pre><code>   use List::MoreUtils qw(uniq);\n\n   my @unique = uniq( 1, 2, 3, 4, 4, 5, 6, 5, 7 ); # 1,2,3,4,5,6,7\n   my $unique = uniq( 1, 2, 3, 4, 4, 5, 6, 5, 7 ); # 7\n</code></pre>\n<p>You can also go through each element and skip the ones you've seen\nbefore. Use a hash to keep track. The first time the loop sees an\nelement, that element has no key in %Seen. The &quot;next&quot; statement creates\nthe key and immediately uses its value, which is &quot;undef&quot;, so the loop\ncontinues to the &quot;push&quot; and increments the value for that key. The next\ntime the loop sees that same element, its key exists in the hash and\nthe value for that key is true (since it's not 0 or &quot;undef&quot;), so the\nnext skips that iteration and the loop goes to the next element.</p>\n<pre><code>   my @unique = ();\n   my %seen   = ();\n\n   foreach my $elem ( @array )\n   {\n     next if $seen{ $elem }++;\n     push @unique, $elem;\n   }\n</code></pre>\n<p>You can write this more briefly using a grep, which does the same thing.</p>\n<pre><code>   my %seen = ();\n   my @unique = grep { ! $seen{ $_ }++ } @array;\n</code></pre>\n"}, {'answer_id': 36739, 'author': 'Ranguard', 'author_id': 3838, 'author_profile': 'https://Stackoverflow.com/users/3838', 'pm_score': 6, 'selected': False, 'text': '<p>Install <a href="http://search.cpan.org/dist/List-MoreUtils/" rel="noreferrer">List::MoreUtils</a> from CPAN</p>\n\n<p>Then in your code:</p>\n\n<pre><code>use strict;\nuse warnings;\nuse List::MoreUtils qw(uniq);\n\nmy @dup_list = qw(1 1 1 2 3 4 4);\n\nmy @uniq_list = uniq(@dup_list);\n</code></pre>\n'}, {'answer_id': 475071, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': "<p>That last one was pretty good. I'd just tweak it a bit:</p>\n\n<pre><code>my @arr;\nmy @uniqarr;\n\nforeach my $var ( @arr ){\n  if ( ! grep( /$var/, @uniqarr ) ){\n     push( @uniqarr, $var );\n  }\n}\n</code></pre>\n\n<p>I think this is probably the most readable way to do it.</p>\n"}, {'answer_id': 4004912, 'author': 'Sreedhar', 'author_id': 485150, 'author_profile': 'https://Stackoverflow.com/users/485150', 'pm_score': 3, 'selected': False, 'text': '<p>The variable <code>@array</code> is the list with duplicate elements</p>\n<pre><code>%seen=();\n@unique = grep { ! $seen{$_} ++ } @array;\n</code></pre>\n'}, {'answer_id': 8071893, 'author': 'Hawk', 'author_id': 1038583, 'author_profile': 'https://Stackoverflow.com/users/1038583', 'pm_score': 3, 'selected': False, 'text': "<p>Can be done with a simple Perl one-liner.</p>\n<pre><code>my @in=qw(1 3 4  6 2 4  3 2 6  3 2 3 4 4 3 2 5 5 32 3); #Sample data \nmy @out=keys %{{ map{$_=&gt;1}@in}}; # Perform PFM\nprint join ' ', sort{$a&lt;=&gt;$b} @out;# Print data back out sorted and in order.\n</code></pre>\n<p>The PFM block does this:</p>\n<p>Data in <code>@in</code> is fed into <code>map</code>. <code>map</code> builds an anonymous hash.  <code>keys</code> are extracted from the hash and feed into <code>@out</code></p>\n"}, {'answer_id': 30448251, 'author': 'saschabeaumont', 'author_id': 592, 'author_profile': 'https://Stackoverflow.com/users/592', 'pm_score': 0, 'selected': False, 'text': '<p>Try this, seems the uniq function needs a sorted list to work properly.</p>\n\n<pre><code>use strict;\n\n# Helper function to remove duplicates in a list.\nsub uniq {\n  my %seen;\n  grep !$seen{$_}++, @_;\n}\n\nmy @teststrings = ("one", "two", "three", "one");\n\nmy @filtered = uniq @teststrings;\nprint "uniq: @filtered\\n";\nmy @sorted = sort @teststrings;\nprint "sort: @sorted\\n";\nmy @sortedfiltered = uniq sort @teststrings;\nprint "uniq sort : @sortedfiltered\\n";\n</code></pre>\n'}, {'answer_id': 43114185, 'author': 'Sandeep_black', 'author_id': 7277468, 'author_profile': 'https://Stackoverflow.com/users/7277468', 'pm_score': 0, 'selected': False, 'text': '<p>Using concept of unique hash keys :</p>\n\n<pre><code>my @array  = ("a","b","c","b","a","d","c","a","d");\nmy %hash   = map { $_ =&gt; 1 } @array;\nmy @unique = keys %hash;\nprint "@unique","\\n";\n</code></pre>\n\n<p>Output:\na c b d </p>\n'}, {'answer_id': 43873983, 'author': 'Kamal Nayan', 'author_id': 4414367, 'author_profile': 'https://Stackoverflow.com/users/4414367', 'pm_score': 3, 'selected': False, 'text': '<h1>Method 1: Use a hash</h1>\n\n<p>Logic: A hash can have only unique keys, so iterate over array, assign any value to each element of array, keeping element as key of that hash. Return keys of the hash, its your unique array.</p>\n\n<pre><code>my @unique = keys {map {$_ =&gt; 1} @array};\n</code></pre>\n\n<h1>Method 2: Extension of method 1 for reusability</h1>\n\n<p>Better to make a subroutine if we are supposed to use this functionality multiple times in our code.</p>\n\n<pre><code>sub get_unique {\n    my %seen;\n    grep !$seen{$_}++, @_;\n}\nmy @unique = get_unique(@array);\n</code></pre>\n\n<h1>Method 3: Use module <code>List::MoreUtils</code></h1>\n\n<pre><code>use List::MoreUtils qw(uniq);\nmy @unique = uniq(@array);\n</code></pre>\n'}, {'answer_id': 54000062, 'author': 'YenForYang', 'author_id': 7314180, 'author_profile': 'https://Stackoverflow.com/users/7314180', 'pm_score': 1, 'selected': False, 'text': "<p>Previous answers pretty much summarize the possible ways of accomplishing this task.</p>\n\n<p>However, I suggest a modification for those who <em>don't</em> care about <em>counting</em> the duplicates, but <em>do</em> care about order.</p>\n\n<pre><code>my @record = qw( yeah I mean uh right right uh yeah so well right I maybe );\nmy %record;\nprint grep !$record{$_} &amp;&amp; ++$record{$_}, @record;\n</code></pre>\n\n<p>Note that the previously suggested <code>grep !$seen{$_}++ ...</code> increments <code>$seen{$_}</code> before negating, so the increment occurs regardless of whether it has already been <code>%seen</code> or not. The above, however, short-circuits when <code>$record{$_}</code> is true, leaving what's been heard once 'off the <code>%record</code>'.</p>\n\n<p>You could also go for this ridiculousness, which takes advantage of autovivification and existence of hash keys:</p>\n\n<pre><code>...\ngrep !(exists $record{$_} || undef $record{$_}), @record;\n</code></pre>\n\n<p>That, however, might lead to some confusion.</p>\n\n<p>And if you care about neither order or duplicate count, you could for another hack using hash slices and the trick I just mentioned:</p>\n\n<pre><code>...\nundef @record{@record};\nkeys %record; # your record, now probably scrambled but at least deduped\n</code></pre>\n"}]
['https://Stackoverflow.com/questions/7651', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/381/']