Duplicate

Today some short codes for the essential task that is de-duplication! :smile:

Python

Starting with python, we use the properties of data containers. Here using an intermediate dictionary:

array = [1, 2, 1, 2, 1, 2]
array = list(dict.fromkeys(array))

Or similar approach using a set:

array = [1, 2, 1, 2, 1, 2]
array = list(set(array))

Perl

The elegant uniq method:

my @dups = (1, 2, 1, 2, 1, 2);
@nodup = uniq @dups;

You have to install and use List::MoreUtils which is a VERY famous Perl CPAN module.

But if you want to do not use a module, here is my “go-to” (no-module) trick:

my @dups = (1, 2, 1, 2, 1, 2);
my @nodup = do { my %seen; grep { !$seen{$_}++ } @dups };

The idea behind is like the first Python (intermediate hash).

Ruby

Ruby takes adventage of the “almost everything in ruby is an object” so it’s simple with the uniq method:

array = [1, 2, 1, 2, 1, 2]
nodup = array.uniq

It’s your turn

Please comment yours :smile: in Python, Perl, Ruby or any other language!