Remove duplicate elements: Difference between revisions
Content added Content deleted
m (Categorizing programming examples) |
|||
Line 10: | Line 10: | ||
==[[Perl]]== |
==[[Perl]]== |
||
[[Category:Perl]] |
|||
'''Interpeter:''' [[Perl]] |
'''Interpeter:''' [[Perl]] |
||
Line 19: | Line 20: | ||
==[[Python]]== |
==[[Python]]== |
||
[[Category:Python]] |
|||
data = [1, 2, 3, 'a', 'b', 'c', 2, 3, 4, 'b', 'c', 'd'] |
data = [1, 2, 3, 'a', 'b', 'c', 2, 3, 4, 'b', 'c', 'd'] |
||
Using sets |
Using sets |
||
Line 25: | Line 27: | ||
==[[Ruby]]== |
==[[Ruby]]== |
||
[[Category:Ruby]] |
|||
ary = [1,1,2,1,'redundant',[1,2,3],[1,2,3],'redundant'] |
ary = [1,1,2,1,'redundant',[1,2,3],[1,2,3],'redundant'] |
||
uniq_ary = ary.uniq |
uniq_ary = ary.uniq |
Revision as of 02:24, 24 January 2007
Remove duplicate elements
You are encouraged to solve this task according to the task description, using any language you may know.
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
Java
//Using Java 1.5/5.0 Object[] data = new Object[] {1, 2, 3, "a", "b", "c", 2, 3, 4, "b", "c", "d"}; Set uniqueSet = new HashSet(Arrays.asList(data)); Object[] unique = uniqueSet.toArray();
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 my @unique_list = keys(%hash);
Python
data = [1, 2, 3, 'a', 'b', 'c', 2, 3, 4, 'b', 'c', 'd']
Using sets
unique = list(set(data))
See also http://www.peterbe.com/plog/uniqifiers-benchmark and http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52560
Ruby
ary = [1,1,2,1,'redundant',[1,2,3],[1,2,3],'redundant'] uniq_ary = ary.uniq # => [1, 2, "redundant", [1, 2, 3]]