Kotlin check list for duplicates

This language bar is your friend. Select your favorite languages!
Select your favorite languages :
  • C
  • C++
  • C#
  • Go
  • Java
  • JS
  • Obj-C
  • PHP
  • Python
  • Ruby
  • Rust
  • Or search :

Idiom #119 Deduplicate list

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

Kotlin check list for duplicates
  • Kotlin
  • Kotlin
  • Clojure
  • C++
  • C++
  • C#
  • D
  • D
  • Dart
  • Elixir
  • Erlang
  • Go
  • Go
  • Go
  • Groovy
  • Haskell
  • JS
  • JS
  • JS
  • Java
  • Java
  • Java
  • Lisp
  • Lua
  • Obj-C
  • PHP
  • Pascal
  • Perl
  • Python
  • Python
  • Ruby
  • Rust
  • Rust
  • Scala
  • Scheme
  • Smalltalk
  • Smalltalk
  • VB
  • Kotlin
x = x.distinct()
  • Demo
  • Doc
  • Kotlin
x = x.toSet().toList()
  • Demo
  • Clojure
  • C++
  • C++
  • C#
  • D
  • D
  • Dart
  • Elixir
  • Erlang
  • Go
  • Go
  • Go
  • Groovy
  • Haskell
  • JS
  • JS
  • JS
  • Java
  • Java
  • Java
  • Lisp
  • Lua
  • Obj-C
  • PHP
  • Pascal
  • Perl
  • Python
  • Python
  • Ruby
  • Rust
  • Rust
  • Scala
  • Scheme
  • Smalltalk
  • Smalltalk
  • VB
(distinct x)
  • Doc
#include #include
std::sort(x.begin(), x.end()); auto last = std::unique(x.begin(), x.end()); x.erase(last, x.end());
  • Demo
  • Doc
#include #include #include
std::vector x = {"one", "two", "two", "one", "three"}; std::unordered_set t; for (auto e : x) t.insert(e);
  • Demo
using System.Collections.Generic; using System.Linq;
var uniques = x.Distinct().ToList();
import std.container; import std.array;
x = redBlackTree(x)[].array;
  • Doc
import std.algorithm; import std.array;
x = x.sort.uniq.array;
  • Doc
x = x.toSet().toList();
  • Demo
  • Origin
Enum.uniq(x)
  • Doc
  • Origin
S = lists:usort(X)
  • Doc
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
  • Demo
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]
  • Demo
seen := make(map[T]bool) j := 0 for _, v := range x { if !seen[v] { x[j] = v j++ seen[v] = true } } x = x[:j]
  • Demo
x.unique()
  • Demo
  • Doc
import Data.List (sort)
nub $ sort x
x = Array.from(new Set(x));
  • Demo
  • Doc
  • Origin
const seen = new Set(); x = x.filter( v => { if(seen.has(v)) return false; seen.add(v); return true; });
  • Demo
  • Doc
  • Origin
x = [...new Set(x)];
  • Demo
  • Doc
  • Origin
import java.util.HashSet; import java.util.ArrayList;
x = new ArrayList(new HashSet(x));
import java.util.HashSet; import java.util.List; import java.util.Set;
Set uniques = new HashSet<>(x); x.clear(); x.addAll(uniques);
import java.util.HashSet; import java.util.Iterator;
final HashSet seen = new HashSet(); final Iterator listIt = x.iterator(); while (listIt.hasNext()) { final T curr = listIt.next(); if (seen.contains(curr)) { listIt.remove(); } else { seen.add(curr); } }
  • Doc
(remove-duplicates x)
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);
  • Doc
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);
  • Doc
from collections import OrderedDict
x = list(OrderedDict(zip(x, x)))
  • Doc
x = list(set(x))
x.uniq!
  • Doc
x.sort(); x.dedup();
  • Demo
  • Doc
use itertools::Itertools;
let dedup: Vec<_> = x.iter().unique().collect();
  • Demo
  • Doc
x = x.distinct
(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)
  • Origin
x asSet.
x intersection: x asSet.
x = x.Distinct.ToList

Do you know the best way to do this in your language ? New implementation...
Idiom created by programming-idioms.org
History
  • View revisions
Related idioms
  • List to set
  • List intersection
Issues
  • Report a bug
Aԁ >

Cool Maze

Kotlin check list for duplicates
Transfer a photo from your phone to your computer