Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Perl

Idiom #118 List to set

Create the set y from the list x.
x may contain duplicates. y is unordered and has no repeated values.

Turning the list [a,b,c,b] into the set {c,a,b}
use List::Util qw(uniq);
my @y = uniq @x;

Filters a list of values to remove subsequent duplicates. Preserves the order of unique elements, and retains the first value of any duplicate set.
my %y = map {$_=>0} @x;

Convert a list to a hash, the value is unimportant, so we used 0.
(def y (set x))

New implementation...
< >
programming-idioms.org