Remove duplicate elements: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
 
Line 2: Line 2:


Given a Array, create a derived Array containing only the unique elements
Given a Array, create a derived Array containing only the unique elements

==[[Perl]]==
'''Interpeter:''' Perl

my %hash;
my @list = (1, 2, 3, 'a', 'b', 'c', 2, 3, 4, 'b', 'c', 'd');
@hash{@list} = 1;
# the keys of %hash now contain the unique list



==[[Ruby]]==
==[[Ruby]]==

Revision as of 21:29, 22 January 2007

Task
Remove duplicate elements
You are encouraged to solve this task according to the task description, using any language you may know.

Given a Array, create a derived Array containing only the unique elements

Perl

Interpeter: Perl

my %hash;
my @list = (1, 2, 3, 'a', 'b', 'c', 2, 3, 4, 'b', 'c', 'd');
@hash{@list} = 1;
# the keys of %hash now contain the unique list


Ruby

 ary = [1,1,2,1,'redundant',[1,2,3],[1,2,3],'redundant']
 uniq_ary = ary.uniq
 # => [1, 2, "redundant", [1, 2, 3]]