Logo

Programming-Idioms

  • Smalltalk
  • Kotlin
  • JS
  • D
import std.container;
import std.array;
x = redBlackTree(x)[].array;

Converts to a set then back to an array
import std.algorithm;
import std.array;
x = x.sort.uniq.array;

uniq takes and output a range which could be infinite so it only looks for adjacent duplicates. That's why we sort x beforehand.
x asSet.

Does not preserve ordering.
x intersection: x asSet.

Preserves order in some Smalltalks (not in Pharo)
x = x.distinct()

Ordering is preserved
x = x.toSet().toList()

original ordering is not preserved
const seen = new Set();
x = x.filter( v => {
  if(seen.has(v))
    return false;
  seen.add(v);
  return true;
});

Original order is preserved.
x = Array.from(new Set(x));

Original order is preserved.
x = [...new Set(x)];

Original order is preserved.
(distinct x)

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