Remove duplicate elements: Difference between revisions

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


Given an Array, derive an Array containing only the unique elements; that is, derive an array in which all duplicate elements are removed.
Given an Array, derive an Array containing only the unique elements; that is, derive an array in which all duplicate elements are removed.



==[[C++]]==
==[[C++]]==
[[Category:C++]]
[[Category:C++]]
<pre>
#include <set>
#include <iostream>
#include <set>
#include <iostream>
using namespace std;
using namespace std;
int main( int argc, char* argv[] ) {

typedef set<int> TyHash;
int main( int argc, char* argv[] ) {
int data[] = {1, 2, 3, 2, 3, 4};
TyHash hash(data, data + 6);
typedef set<int> TyHash;
cout << "Set items:" << endl;
int data[] = {1, 2, 3, 2, 3, 4};

for (TyHash::iterator iter = hash.begin(); iter != hash.end(); iter++)
TyHash hash(data, data + 6);

cout << "Set items:" << endl;
for (TyHash::iterator iter = hash.begin(); iter != hash.end(); iter++)
cout << *iter << " ";
cout << *iter << " ";
cout << endl;
cout << endl;
}
}
</pre>


==[[Java]]==
==[[Java]]==
Line 54: Line 58:
==[[D]]==
==[[D]]==
[[Category:D]]
[[Category:D]]
<pre>
void main() {
void main() {
int[] data = [1, 2, 3, 2, 3, 4];
int[] data = [1, 2, 3, 2, 3, 4];
Line 60: Line 65:
hash[el] = 0;
hash[el] = 0;
}
}
</pre>

Revision as of 15:24, 24 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 an Array, derive an Array containing only the unique elements; that is, derive an array in which all duplicate elements are removed.

C++

#include <set>
#include <iostream>
using namespace std;

int main( int argc, char* argv[] ) {
    typedef set<int> TyHash;
    int data[] = {1, 2, 3, 2, 3, 4};

    TyHash hash(data, data + 6);

    cout << "Set items:" << endl;
    for (TyHash::iterator iter = hash.begin(); iter != hash.end(); iter++)
          cout << *iter << " ";
    cout << endl;
}

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);

PHP

 $list = array(1, 2, 3, 'a', 'b', 'c', 2, 3, 4, 'b', 'c', 'd');
 $unique_list = array_unique($list);

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]]

D

void main() {
    int[] data = [1, 2, 3, 2, 3, 4];
    int[int] hash;
    foreach(el; data)
        hash[el] = 0;
}