De-duplicate arrays in Python, Perl and Ruby

Tib - Mar 16 '21 - - Dev Community

Duplicate

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

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))
Enter fullscreen mode Exit fullscreen mode

Or similar approach using a set:

array = [1, 2, 1, 2, 1, 2]
array = list(set(array))
Enter fullscreen mode Exit fullscreen mode

Perl

The elegant uniq method:

my @dups = (1, 2, 1, 2, 1, 2);
@nodup = uniq @dups;
Enter fullscreen mode Exit fullscreen mode

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 };
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

It's your turn

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

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .