# Idiom #119 Deduplicate list

Remove duplicates from the list x.
Explain if the original order is preserved.

(define (remove-duplicates l)
(cond ((null? l)
'())
((member (car l) (cdr l))
(remove-duplicates (cdr l)))
(else
(cons (car l) (remove-duplicates (cdr l))))))
(remove-duplicates x)
(distinct x)
#include <algorithm>
#include <vector>
std::sort(x.begin(), x.end());
auto last = std::unique(x.begin(), x.end());
x.erase(last, x.end());
#include <string>
#include <unordered_set>
#include <vector>
std::vector<std::string> x = {"one", "two", "two", "one", "three"};
std::unordered_set<std::string> t;
for (auto e : x)
t.insert(e);
using System.Collections.Generic;
using System.Linq;
var uniques = x.Distinct().ToList();
import std.algorithm;
import std.array;
x = x.sort.uniq.array;
import std.container;
import std.array;
x = redBlackTree(x)[].array;
x = x.toSet().toList();
Enum.uniq(x)
S = lists:usort(X)
y := make(map[T]struct{}, len(x))
for _, v := range x {
y[v] = struct{}{}
}
x2 := make([]T, 0, len(y))
for _, v := range x {
if _, ok := y[v]; ok {
x2 = append(x2, v)
delete(y, v)
}
}
x = x2
seen := make(map[T]bool)
j := 0
for _, v := range x {
if !seen[v] {
x[j] = v
j++
seen[v] = true
}
}
for i := j; i < len(x); i++ {
x[i] = nil
}
x = x[:j]
func deduplicate[S ~[]T, T comparable](x S) S {
seen := make(map[T]bool)
j := 0
for _, v := range x {
if !seen[v] {
x[j] = v
j++
seen[v] = true
}
}
var zero T
for i := j; i < len(x); i++ {
// Avoid memory leak
x[i] = zero
}
return x[:j]
}
seen := make(map[T]bool)
j := 0
for _, v := range x {
if !seen[v] {
x[j] = v
j++
seen[v] = true
}
}
x = x[:j]
x.unique()
import Data.List (nub)
nub x
x = [...new Set(x)];
x = Array.from(new Set(x));
const seen = new Set();
x = x.filter( v => {
if(seen.has(v))
return false;
return true;
});
import java.util.HashSet;
import java.util.Iterator;
final HashSet<T> seen = new HashSet<T>();
final Iterator<T> listIt = x.iterator();
while (listIt.hasNext()) {
final T curr = listIt.next();
if (seen.contains(curr)) {
listIt.remove();
} else {
}
}
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Set<T> uniques = new HashSet<>(x);
x.clear();
import java.util.HashSet;
import java.util.ArrayList;
x = new ArrayList<T>(new HashSet<T>(x));
x = x.distinct()
x = x.toSet().toList()
(remove-duplicates x)
{
"aim_ag_texture2": [
{
"name": ["T Spawn", "Edge"],
"position": [-772.95001220703, 1647.3299560547, 1.0299999713898],
"viewangles": [-16.03, -85.29]
},
{
"name": ["CT Spawn", "Boxes"],
"weapon": "weapon_molotov",
"position": [-928.53002929688, 80.029998779297, 1.0299999713898],
"viewangles": [-14.58, 64.53],
"run": 13
}
},
{
"name": ["T Spawn", "Edge"],

local seen = {}
for index,item in ipairs(x) do
if seen[item] then
table.remove(x, index)
else
seen[item] = true
end
end
@import Foundation;
[NSSet setWithArray:x].allObjects
\$x = array_unique(\$x);
uses classes;
var
x: TList;
begin
for i:= x.count-1 downto 0 do
if x.indexOf(x.items[i]) <> -1 then
x.delete(i);
end;
use List::MoreUtils 'uniq';
@x = uniq(@x);
from collections import OrderedDict
x = list(OrderedDict(zip(x, x)))
def dedup(x):
y = []
for i in x:
if not i in y:
y.append(i)
return y
x = list(set(x))
x.uniq!
use itertools::Itertools;
let dedup: Vec<_> = x.iter().unique().collect();
x.sort();
x.dedup();
x = x.distinct
x asSet.
x intersection: x asSet.
x = x.Distinct.ToList

programming-idioms.org