Remove duplicate elements: Difference between revisions

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
Line 14:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">V items = [‘1’, ‘2’, ‘3’, ‘a’, ‘b’, ‘c’, ‘2’, ‘3’, ‘4’, ‘b’, ‘c’, ‘d’]
V unique = Array(Set(items))
print(unique)</langsyntaxhighlight>
 
{{out}}
Line 25:
=={{header|360 Assembly}}==
{{trans|PL/I}}
<langsyntaxhighlight lang="360asm">* Remove duplicate elements - 18/10/2015
REMDUP CSECT
USING REMDUP,R15 set base register
Line 72:
PG DC CL92' '
YREGS
END REMDUP</langsyntaxhighlight>
{{out}}
<pre> 6 1 5 2 7 22 4 19 8 9 10 11 12</pre>
Line 80:
This routine works on arrays of bytes, and keeps track of which bytes it has seen in a 256-byte array.
 
<langsyntaxhighlight lang="8080asm"> org 100h
jmp test
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Line 135:
nl: db 13,10,'$'
bufdef: db 127,0
buf:</langsyntaxhighlight>
 
 
=={{header|ACL2}}==
<syntaxhighlight lang ="lisp">(remove-duplicates xs)</langsyntaxhighlight>
 
=={{header|Action!}}==
{{libheader|Action! Tool Kit}}
<langsyntaxhighlight Actionlang="action!">INCLUDE "D2:SORT.ACT" ;from the Action! Tool Kit
 
PROC PrintArray(INT ARRAY a INT size)
Line 202:
Test(src2,26)
Test(src3,1)
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Remove_duplicate_elements.png Screenshot from Atari 8-bit computer]
Line 225:
=={{header|Ada}}==
{{works with|GNAT|GPL 2018}}
<langsyntaxhighlight lang="ada">with Ada.Containers.Ordered_Sets, Ada.Text_IO;
use Ada.Text_IO;
Line 239:
Put (e'img);
end loop;
end Duplicate;</langsyntaxhighlight>
 
=={{header|Aime}}==
Using an index:
<langsyntaxhighlight lang="aime">index x;
 
list(1, 2, 3, 1, 2, 3, 4, 1).ucall(i_add, 1, x, 0);
x.i_vcall(o_, 1, " ");
o_newline();</langsyntaxhighlight>
{{out}}
<pre> 1 2 3 4</pre>
Order preserving solution:
<langsyntaxhighlight lang="aime">index x;
 
for (, integer a in list(8, 2, 1, 8, 2, 1, 4, 8)) {
Line 258:
}
}
o_newline();</langsyntaxhighlight>
{{out}}
<pre> 8 2 1 4</pre>
Line 264:
=={{header|ALGOL 68}}==
Using the associative array code from [[Associative_array/Iteration#ALGOL_68]]
<langsyntaxhighlight lang="algol68"># use the associative array in the Associate array/iteration task #
# this example uses strings - for other types, the associative #
# array modes AAELEMENT and AAKEY should be modified as required #
Line 295:
# test the duplicate removal #
print( ( remove duplicates( ( "A", "B", "D", "A", "C", "F", "F", "A" ) ), newline ) )
</syntaxhighlight>
</lang>
=={{header|Amazing Hopper}}==
<syntaxhighlight lang="amazing hopper">
<lang Amazing Hopper>
#include <hopper.h>
 
Line 316:
{"Final array:\n",y}, println
exit(0)
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 334:
{{works with|GNU APL}}
The primitive monad ∪ means "unique", so:
<langsyntaxhighlight lang="apl">∪ 1 2 3 1 2 3 4 1
1 2 3 4</langsyntaxhighlight>
 
{{works with|APL2}}
{{works with|GNU APL}}
<langsyntaxhighlight lang="apl">w←1 2 3 1 2 3 4 1
((⍳⍨w)=⍳⍴w)/w
1 2 3 4</langsyntaxhighlight>
 
=={{header|AppleScript}}==
===Idiomatic===
<langsyntaxhighlight lang="applescript">unique({1, 2, 3, "a", "b", "c", 2, 3, 4, "b", "c", "d"})
 
on unique(x)
Line 353:
end repeat
return R
end unique</langsyntaxhighlight>
----
===Idiomatic 2 (more robust)===
While it's quite common to see 'is in', 'is not in', 'contains', and 'does not contain' used in the above way for convenience when the scripter knows the code will only be used to check for simple objects, the commands actually compare ''sections of list'' rather than individual elements.
 
<langsyntaxhighlight lang="applescript">{1, 2, 3, {4, 5}} contains {2, 3} --> true
{1, 2, 3, {4, 5}} contains {4, 5} --> false
{1, 2, 3, {4, 5}} contains {{4, 5}} --> true</langsyntaxhighlight>
 
If the search term presented is anything other than a list, it's silently coerced to one for the search.
 
<langsyntaxhighlight lang="applescript">3 is in {1, 2, 3, {4, 5}} --> {3} is in {1, 2, 3, {4, 5}} --> true</langsyntaxhighlight>
 
So for robustness in case the elements sought may already be lists, or records, it's best to wrap them explicitly in list braces.
 
<langsyntaxhighlight lang="applescript">unique({1, 2, 3, "a", "b", "c", 2, 3, 4, "c", {b:"c"}, {"c"}, "c", "d"})
 
on unique(x)
Line 377:
end repeat
return R
end unique</langsyntaxhighlight>
 
{{output}}
<langsyntaxhighlight lang="applescript">{1, 2, 3, "a", "b", "c", 4, {b:"c"}, {"c"}, "d"}</langsyntaxhighlight>
----
===Functional===
Line 391:
{{trans|Haskell}}
 
<langsyntaxhighlight AppleScriptlang="applescript">-- CASE-INSENSITIVE UNIQUE ELEMENTS ------------------------------------------
 
-- nub :: [a] -> [a]
Line 482:
set my text item delimiters to dlm
return lstParts
end splitOn</langsyntaxhighlight>
{{Out}}
 
<langsyntaxhighlight AppleScriptlang="applescript">{"4 3 2 8 0 1 9 5 7 6", "abc "}</langsyntaxhighlight>
----
===AppleScriptObjC===
<langsyntaxhighlight lang="applescript">use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
use framework "Foundation"
 
set aList to {1, 2, 3, "a", "b", "c", 2, 3, 4, "c", {b:"c"}, {"c"}, "c", "d"}
set orderedSet to current application's class "NSOrderedSet"'s orderedSetWithArray:(aList)
return orderedSet's array() as list</langsyntaxhighlight>
 
{{output}}
<langsyntaxhighlight lang="applescript">{1, 2, 3, "a", "b", "c", 4, {b:"c"}, {"c"}, "d"}</langsyntaxhighlight>
 
=={{header|Applesoft BASIC}}==
<langsyntaxhighlight lang="basic">100 DIM L$(15)
110 L$(0) = "NOW"
120 L$(1) = "IS"
Line 539:
520 L$(N) = ""
530 N = N - 1
540 RETURN</langsyntaxhighlight>
 
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">arr: [1 2 3 2 1 2 3 4 5 3 2 1]
print unique arr</langsyntaxhighlight>
 
{{out}}
Line 553:
=={{header|AutoHotkey}}==
Built in Sort has an option to remove duplicates
<langsyntaxhighlight AutoHotkeylang="autohotkey">a = 1,2,1,4,5,2,15,1,3,4
Sort, a, a, NUD`,
MsgBox % a ; 1,2,3,4,5,15</langsyntaxhighlight>
 
=={{header|AWK}}==
Line 563:
then produce a string with the keys of b,
which is finally output.
<langsyntaxhighlight lang="awk">$ awk 'BEGIN{split("a b c d c b a",a);for(i in a)b[a[i]]=1;r="";for(i in b)r=r" "i;print r}'
a b c d</langsyntaxhighlight>
 
 
=={{header|BASIC256}}==
{{trans|True BASIC}}
<syntaxhighlight lang="basic256">
<lang BASIC256>
arraybase 1
max = 10
Line 598:
next i
end
</syntaxhighlight>
</lang>
 
 
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<langsyntaxhighlight lang="bbcbasic"> DIM list$(15)
list$() = "Now", "is", "the", "time", "for", "all", "good", "men", \
\ "to", "come", "to", "the", "aid", "of", "the", "party."
Line 623:
IF j%>=i% l$(n%) = i$ : n% += 1
NEXT
= n%</langsyntaxhighlight>
{{out}}
<pre>
Line 633:
 
The list contains atoms and also a few non-atomic expressions. The hash table needs atomic keys, so we apply the <code>str</code> function when searching and inserting elements.
<langsyntaxhighlight lang="bracmat">2 3 5 7 11 13 17 19 cats 222 (-100.2) "+11" (1.1) "+7" (7.) 7 5 5 3 2 0 (4.4) 2:?LIST
 
(A=
Line 695:
& !C
&
)</langsyntaxhighlight>
Only solution B produces a list with the same order of elements as in the input.
<pre>Solution A: 19 (4.4) 17 11 13 (1.1) (7.) 222 +11 7 5 3 2 0 cats (-100.2) +7
Line 702:
 
=={{header|Brat}}==
<langsyntaxhighlight lang="brat">some_array = [1 1 2 1 'redundant' [1 2 3] [1 2 3] 'redundant']
 
unique_array = some_array.unique</langsyntaxhighlight>
 
=={{header|C}}==
Line 712:
Since there's no way to know ahead of time how large the new data structure will need to be, we'll return a linked list instead of an array.
 
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
 
Line 744:
printf("%d ", n->x);
puts("");
return 0;}</langsyntaxhighlight>
 
{{out}}
Line 751:
===O(n^2) version, pure arrays===
 
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
Line 809:
free(b);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 816:
===Sorting method===
 
Using qsort and return uniques in-place:<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
 
Line 844:
 
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 856:
=={{header|C sharp}}==
{{works with |C sharp|C#|2+}}
<langsyntaxhighlight lang="csharp">int[] nums = { 1, 1, 2, 3, 4, 4 };
List<int> unique = new List<int>();
foreach (int n in nums)
if (!unique.Contains(n))
unique.Add(n);</langsyntaxhighlight>
 
{{works with |C sharp|C#|3+}}
<langsyntaxhighlight lang="csharp">int[] nums = {1, 1, 2, 3, 4, 4};
int[] unique = nums.Distinct().ToArray();</langsyntaxhighlight>
 
=={{header|C++}}==
This version uses <tt>std::set</tt>, which requires its element type be comparable using the < operator.
<langsyntaxhighlight lang="cpp">#include <set>
#include <iostream>
using namespace std;
Line 882:
cout << *iter << " ";
cout << endl;
}</langsyntaxhighlight>
 
This version uses <tt>hash_set</tt>, which is part of the SGI extension to the Standard Template Library. It is not part of the C++ standard library. It requires that its element type have a hash function.
 
{{works with|GCC}}
<langsyntaxhighlight lang="cpp">#include <ext/hash_set>
#include <iostream>
using namespace std;
Line 901:
cout << *iter << " ";
cout << endl;
}</langsyntaxhighlight>
 
This version uses <tt>unordered_set</tt>, which is part of the TR1, which is likely to be included in the next version of C++. It is not part of the C++ standard library. It requires that its element type have a hash function.
 
{{works with|GCC}}
<langsyntaxhighlight lang="cpp">#include <tr1/unordered_set>
#include <iostream>
using namespace std;
Line 920:
cout << *iter << " ";
cout << endl;
}</langsyntaxhighlight>
 
Alternative method working directly on the array:
 
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <iterator>
#include <algorithm>
Line 938:
std::copy(data, new_end, std::ostream_iterator<int>(std::cout, " ");
std::cout << std::endl;
}</langsyntaxhighlight>
 
Using sort, unique, and erase on a vector.
{{works with|C++11}}
<langsyntaxhighlight lang="cpp">#include <algorithm>
#include <iostream>
#include <vector>
Line 955:
std::cout << std::endl;
return 0;
}</langsyntaxhighlight>
 
=={{header|CafeOBJ}}==
Line 1,016:
 
=={{header|Ceylon}}==
<langsyntaxhighlight lang="ceylon"><String|Integer>[] data = [1, 2, 3, "a", "b", "c", 2, 3, 4, "b", "c", "d"];
<String|Integer>[] unique = HashSet { *data }.sequence();</langsyntaxhighlight>
 
=={{header|Clojure}}==
 
<langsyntaxhighlight lang="lisp">user=> (distinct [1 3 2 9 1 2 3 8 8 1 0 2])
(1 3 2 9 8 0)
user=></langsyntaxhighlight>
 
=={{header|CoffeeScript}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="coffeescript">data = [ 1, 2, 3, "a", "b", "c", 2, 3, 4, "b", "c", "d" ]
set = []
set.push i for i in data when not (i in set)
 
console.log data
console.log set</langsyntaxhighlight>
{{out}}
<pre>[ 1, 2, 3, 'a', 'b', 'c', 2, 3, 4, 'b', 'c', 'd' ]
Line 1,041:
To remove duplicates non-destructively:
 
<langsyntaxhighlight lang="lisp">(remove-duplicates '(1 3 2 9 1 2 3 8 8 1 0 2))
> (9 3 8 1 0 2)</langsyntaxhighlight>
 
Or, to remove duplicates in-place:
 
<langsyntaxhighlight lang="lisp">(delete-duplicates '(1 3 2 9 1 2 3 8 8 1 0 2))
> (9 3 8 1 0 2)</langsyntaxhighlight>
 
=={{header|Crystal}}==
Line 1,053:
Copied and modified from the Ruby version.
 
<langsyntaxhighlight lang="ruby">ary = [1, 1, 2, 2, "a", [1, 2, 3], [1, 2, 3], "a"]
p ary.uniq</langsyntaxhighlight>
 
<pre>[1, 2, "a", [1, 2, 3]]</pre>
 
=={{header|D}}==
<langsyntaxhighlight lang="d">void main() {
import std.stdio, std.algorithm;
 
Line 1,066:
.uniq
.writeln;
}</langsyntaxhighlight>
{{out}}
<pre>[0, 1, 2, 3, 8, 9]</pre>
Using an associative array:
<langsyntaxhighlight lang="d">void main() {
import std.stdio;
 
Line 1,079:
hash[el] = true;
hash.byKey.writeln;
}</langsyntaxhighlight>
{{out}}
<pre>[8, 0, 1, 9, 2, 3]</pre>
Like code D#1, but with an array returned:
<langsyntaxhighlight lang="d">void main()
{
import std.stdio, std.algorithm, std.array;
Line 1,089:
auto a = [5,4,32,7,6,4,2,6,0,8,6,9].sort.uniq.array;
a.writeln;
}</langsyntaxhighlight>
{{out}}
<pre>[0, 2, 4, 5, 6, 7, 8, 9, 32]</pre>
Line 1,096:
Generics were added in Delphi2009.
 
<langsyntaxhighlight Delphilang="delphi">program RemoveDuplicateElements;
 
{$APPTYPE CONSOLE}
Line 1,119:
lIntegerList.Free;
end;
end.</langsyntaxhighlight>
 
{{out}}
Line 1,130:
=={{header|Déjà Vu}}==
 
<langsyntaxhighlight lang="dejavu">}
for item in [ 1 10 1 :hi :hello :hi :hi ]:
@item
!. keys set{</langsyntaxhighlight>
{{out}}
<pre>[ 1 :hello 10 :hi ]</pre>
Line 1,139:
=={{header|E}}==
 
<langsyntaxhighlight lang="e">[1,2,3,2,3,4].asSet().getElements()</langsyntaxhighlight>
 
=={{header|ECL}}==
<langsyntaxhighlight lang="ecl">
inNumbers := DATASET([{1},{2},{3},{4},{1},{1},{7},{8},{9},{9},{0},{0},{3},{3},{3},{3},{3}], {INTEGER Field1});
DEDUP(SORT(inNumbers,Field1));
</syntaxhighlight>
</lang>
{{out}}
<pre>0
Line 1,159:
=={{header|Elena}}==
ELENA 5.0 :
<langsyntaxhighlight lang="elena">import extensions;
import system'collections;
import system'routines;
Line 1,171:
console.printLine(unique.MapValues.asEnumerable())
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,180:
Elixir has an <code>Enum.uniq</code> built-in function.
{{works with|Elixir|1.2}}
<langsyntaxhighlight lang="elixir">defmodule RC do
# Set approach
def uniq1(list), do: MapSet.new(list) |> MapSet.to_list
Line 1,206:
end)
|> fn{t,_} -> IO.puts "#{inspect fun}:\t#{t/1000000}\t#{inspect result}" end.()
end)</langsyntaxhighlight>
 
{{out}}
Line 1,218:
=={{header|Erlang}}==
 
<langsyntaxhighlight lang="erlang">List = [1, 2, 3, 2, 2, 4, 5, 5, 4, 6, 6, 5].
UniqueList = gb_sets:to_list(gb_sets:from_list(List)).
% Alternatively the builtin:
Unique_list = lists:usort( List ).
</syntaxhighlight>
</lang>
 
=={{header|Euphoria}}==
<langsyntaxhighlight lang="euphoria">include sort.e
 
function uniq(sequence s)
Line 1,241:
constant s = {1, 2, 1, 4, 5, 2, 15, 1, 3, 4}
? s
? uniq(s)</langsyntaxhighlight>
 
{{out}}
Line 1,250:
=={{header|F Sharp|F#}}==
The simplest way is to build a set from the given array (this actually works for any enumerable input sequence type, not just arrays):
<langsyntaxhighlight lang="fsharp">
set [|1;2;3;2;3;4|]
</syntaxhighlight>
</lang>
gives:
<langsyntaxhighlight lang="fsharp">
val it : Set<int> = seq [1; 2; 3; 4]
</syntaxhighlight>
</lang>
 
=={{header|Factor}}==
 
<langsyntaxhighlight lang="factor">USING: sets ;
V{ 1 2 1 3 2 4 5 } members .
 
V{ 1 2 3 4 5 }</langsyntaxhighlight>
 
=={{header|Forth}}==
Line 1,273:
The input data is assumed to be sorted.
 
<langsyntaxhighlight lang="forth">\ Increments a2 until it no longer points to the same value as a1
\ a3 is the address beyond the data a2 is traversing.
: skip-dups ( a1 a2 a3 -- a1 a2+n )
Line 1,291:
cell+ r@ skip-dups
cell 0 d+ \ increment write ptr only
repeat r> 2drop r> - cell / ;</langsyntaxhighlight>
 
Here is another implementation of "uniq" that uses a popular parameters and local variables extension words. It is structurally the same as the above implementation, but uses less overt stack manipulation.
 
<langsyntaxhighlight lang="forth">: uniqv { a n \ r e -- n }
a n cells+ to e
a dup to r
Line 1,306:
cell+
repeat
a - cell / ;</langsyntaxhighlight>
 
To test this code, you can execute:
 
<langsyntaxhighlight lang="forth">create test 1 , 2 , 3 , 2 , 6 , 4 , 5 , 3 , 6 ,
here test - cell / constant ntest
: .test ( n -- ) 0 ?do test i cells + ? loop ;
 
test ntest 2dup cell-sort uniq .test</langsyntaxhighlight>
 
{{out}}
Line 1,322:
Fortran has no built-in hash functions or sorting functions but the code below implements the compare all elements algorithm.
 
<langsyntaxhighlight lang="fortran">
 
program remove_dups
Line 1,349:
end program remove_dups
 
</syntaxhighlight>
</lang>
 
Same as above but using 'ANY' to check if the input number already exists in the array of unique elements:
 
<langsyntaxhighlight lang="fortran">
program remove_dups
implicit none
Line 1,376:
end program remove_dups
 
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,382:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
Sub removeDuplicates(a() As Integer, b() As Integer)
Line 1,420:
Print
Print "Press any key to quit"
Sleep</langsyntaxhighlight>
 
{{out}}
Line 1,429:
=={{header|Frink}}==
The following demonstrates two of the simplest ways of removing duplicates.
<langsyntaxhighlight lang="frink">
b = [1, 5, 2, 6, 6, 2, 2, 1, 9, 8, 6, 5]
 
Line 1,439:
// Another way, using the "set" datatype and back to an array.
println[toArray[toSet[b]]
</syntaxhighlight>
</lang>
 
{{out}} Note that sets are not guaranteed to be printed in any specific order.
Line 1,449:
 
=={{header|Futurebasic}}==
<langsyntaxhighlight lang="futurebasic">
include "NSLog.incl"
 
Line 1,460:
 
HandleEvents
</syntaxhighlight>
</lang>
{{output}}
<pre>
Line 1,477:
=={{header|Gambas}}==
'''[https://gambas-playground.proko.eu/?gist=1e2bb524d2278cd88bccdc21a1683296 Click this link to run this code]'''
<langsyntaxhighlight lang="gambas">Public Sub Main()
Dim sString As String[] = Split("Now is the time for all the good men to come to the aid of the good party 1 2 1 3 3 3 2 1 1 2 3 4 33 2 5 4 333 5", " ")
Dim sFix As New String[]
Line 1,490:
Print sFix.Join(" ")
 
End</langsyntaxhighlight>
Output:
<pre>
Line 1,497:
 
=={{header|GAP}}==
<langsyntaxhighlight lang="gap"># Built-in, using sets (which are also lists)
a := [ 1, 2, 3, 1, [ 4 ], 5, 5, [4], 6 ];
# [ 1, 2, 3, 1, [ 4 ], 5, 5, [ 4 ], 6 ]
Line 1,505:
# true
IsList(b);
# true</langsyntaxhighlight>
 
=={{header|Go}}==
===Map solution===
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 1,527:
func main() {
fmt.Println(uniq([]int{1, 2, 3, 2, 3, 4})) // prints: [3 4 1 2] (but in a semi-random order)
}</langsyntaxhighlight>
===Map preserving order===
It takes only small changes to the above code to preserver order. Just store the sequence in the map:
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 1,552:
func main() {
fmt.Println(uniq([]int{1, 2, 3, 2, 3, 4})) // prints: [1 2 3 4]
}</langsyntaxhighlight>
===Float64, removing duplicate NaNs===
In solutions above, you just replace <code>int</code> with another type to use for a list of another type. (See [[Associative_arrays/Creation#Go]] for acceptable types.) Except a weird thing happens with NaNs. They (correctly) don't compare equal, so you have to special case them if you want to remove duplicate NaNs:
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,589:
func main() {
fmt.Println(uniq([]float64{1, 2, math.NaN(), 2, math.NaN(), 4})) // Prints [1 2 NaN 4]
}</langsyntaxhighlight>
===Any type using reflection===
Go doesn't have templates or generics, but it does have reflection.
Line 1,598:
 
Note: due to details with how Go handles map keys that contain a NaN somewhere (including within a complex or even within a sub struct field) this version simply omits any NaN containing values it comes across and returns a bool to indicate if that happened. This version is otherwise a translation of the above order preserving map implementation, it does not for example call reflect.DeepEqual so elements with pointers to distinct but equal values will be treated as non-equal.
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,687:
//uniq(a)
//uniq(nil)
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,700:
 
=={{header|Groovy}}==
<langsyntaxhighlight lang="groovy">def list = [1, 2, 3, 'a', 'b', 'c', 2, 3, 4, 'b', 'c', 'd']
assert list.size() == 12
println " Original List: ${list}"
Line 1,721:
def set = list as Set
assert set.size() == 8
println " Set: ${set}"</langsyntaxhighlight>
 
{{out}}
Line 1,732:
{{trans|Modula-2}}
{{works with|PC-BASIC|any}}
<langsyntaxhighlight lang="qbasic">
10 ' Remove Duplicates
20 OPTION BASE 1
Line 1,760:
260 END
1000 DATA 1, 2, 2, 3, 4, 5, 5
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,774:
===Usage===
 
<langsyntaxhighlight lang="haskell"> print $ unique [4, 5, 4, 2, 3, 3, 4]
 
[4,5,2,3]</langsyntaxhighlight>
 
===Sorted result using Set===
Line 1,782:
O(n ln(n)). Requires there is a partial ordering of elements.
 
<langsyntaxhighlight lang="haskell">import qualified Data.Set as Set
 
unique :: Ord a => [a] -> [a]
unique = Set.toList . Set.fromList</langsyntaxhighlight>
 
===Unsorted result using Set===
Line 1,791:
O(n ln(n)). Retains original order. Requires there is a partial ordering of elements.
 
<langsyntaxhighlight lang="haskell">import Data.Set
 
unique :: Ord a => [a] -> [a]
Line 1,798:
loop s [] = []
loop s (x : xs) | member x s = loop s xs
| otherwise = x : loop (insert x s) xs</langsyntaxhighlight>
 
===Using filter===
Line 1,804:
O(n^2). Retains original order. Only requires that elements can be compared for equality.
 
<langsyntaxhighlight lang="haskell">import Data.List
 
unique :: Eq a => [a] -> [a]
unique [] = []
unique (x : xs) = x : unique (filter (x /=) xs)</langsyntaxhighlight>
 
===Standard Library===
 
<langsyntaxhighlight lang="haskell">import Data.List
Data.List.nub :: Eq a => [a] -> [a]
Data.List.Unique.unique :: Ord a => [a] -> [a]</langsyntaxhighlight>
 
=={{header|HicEst}}==
<langsyntaxhighlight lang="hicest">REAL :: nums(12)
CHARACTER :: workspace*100
 
Line 1,825:
READ(Text=workspace, ItemS=individuals) nums ! convert to numeric
 
WRITE(ClipBoard) individuals, "individuals: ", nums ! 6 individuals: 0 1 2 3 8 9 0 0 0 0 0 0 </langsyntaxhighlight>
 
=={{header|Icon}} and {{header|Unicon}}==
This solution preserves the original order of the elements.
<langsyntaxhighlight Iconlang="icon">procedure main(args)
every write(!noDups(args))
end
Line 1,843:
return a
}
end</langsyntaxhighlight>
A sample run is:
<pre>
Line 1,857:
=={{header|IDL}}==
 
<langsyntaxhighlight lang="idl">non_repeated_values = array[uniq(array, sort( array))]</langsyntaxhighlight>
 
=={{header|Inform 7}}==
 
<langsyntaxhighlight lang="inform7">To decide which list of Ks is (L - list of values of kind K) without duplicates:
let result be a list of Ks;
repeat with X running through L:
add X to result, if absent;
decide on result.</langsyntaxhighlight>
 
=={{header|IS-BASIC}}==
<langsyntaxhighlight ISlang="is-BASICbasic">100 PROGRAM "RemoveDu.bas"
110 RANDOMIZE
120 NUMERIC ARR(1 TO 20),TOP
Line 1,894:
340 NEXT
350 LET REMOVE=UBOUND(A)-ST
360 END DEF</langsyntaxhighlight>
 
{{out}}
Line 1,920:
=={{header|J}}==
The verb<code> ~. </code>removes duplicate items from ''any'' array (numeric, character, or other; vector, matrix, rank-n array). For example:
<langsyntaxhighlight lang="j"> ~. 4 3 2 8 0 1 9 5 1 7 6 3 9 9 4 2 1 5 3 2
4 3 2 8 0 1 9 5 7 6
~. 'chthonic eleemosynary paronomasiac'
chtoni elmsyarp</langsyntaxhighlight>
Or (since J defines an item of an n dimensional array as its n-1 dimensional sub arrays):
 
<langsyntaxhighlight lang="j"> 0 1 1 2 0 */0 1 2
0 0 0
0 1 2
Line 1,935:
0 0 0
0 1 2
0 2 4</langsyntaxhighlight>
 
=={{header|Java}}==
{{works with|Java|1.5}}
<langsyntaxhighlight lang="java5">import java.util.*;
 
class Test {
Line 1,950:
System.out.printf("%s ", o);
}
}</langsyntaxhighlight>
<pre>1 a 2 b 3 c d</pre>
 
{{works with|Java|8}}
<langsyntaxhighlight lang="java">import java.util.*;
 
class Test {
Line 1,963:
Arrays.stream(data).distinct().forEach((o) -> System.out.printf("%s ", o));
}
}</langsyntaxhighlight>
<pre>1 2 3 a b c d</pre>
 
=={{header|JavaScript}}==
This uses the <code>===</code> "strict equality" operator, which does no type conversions (<code>4 == "4"</code> is true but <code>4 === "4"</code> is false)
<langsyntaxhighlight lang="javascript">function unique(ary) {
// concat() with no args is a way to clone an array
var u = ary.concat().sort();
Line 1,983:
var uniq = unique(ary);
for (var i = 0; i < uniq.length; i++)
print(uniq[i] + "\t" + typeof(uniq[i]));</langsyntaxhighlight>
<pre>1 - number
2 - number
Line 1,995:
 
Or, extend the prototype for Array:
<langsyntaxhighlight lang="javascript">Array.prototype.unique = function() {
var u = this.concat().sort();
for (var i = 1; i < u.length; ) {
Line 2,005:
return u;
}
var uniq = [1, 2, 3, "a", "b", "c", 2, 3, 4, "b", "c", "d"].unique();</langsyntaxhighlight>
 
With reduce and arrow functions (ES6):
<langsyntaxhighlight lang="javascript">Array.prototype.unique = function() {
return this.sort().reduce( (a,e) => e === a[a.length-1] ? a : (a.push(e), a), [] )
}</langsyntaxhighlight>
 
With sets and spread operator (ES6):
<langsyntaxhighlight lang="javascript">Array.prototype.unique = function() {
return [... new Set(this)]
}</langsyntaxhighlight>
 
If, however, the array is homogenous, or we wish to interpret it as such by using JavaScript's Abstract Equality comparison (as in '==', see http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3) then it proves significantly faster to use a hash table.
Line 2,021:
For example, in ES 5:
 
<langsyntaxhighlight JavaScriptlang="javascript">function uniq(lst) {
var u = [],
dct = {},
Line 2,035:
return u;
}</langsyntaxhighlight>
 
Or, to allow for customised definitions of equality and duplication, we can follow the Haskell prelude in defining a '''nub :: [a] -> [a] function''' which is a special case of '''nubBy :: (a -> a -> Bool) -> [a] -> [a]'''
Line 2,041:
{{trans|Haskell}}
 
<langsyntaxhighlight JavaScriptlang="javascript">(function () {
'use strict';
 
Line 2,079:
]
 
})();</langsyntaxhighlight>
 
{{Out}}
Line 2,088:
If it is acceptable to alter the ordering of elements, then
the builtin (fast) filter, '''unique''', can be used. It can be used for arrays with elements of any JSON type and returns the distinct elements in sorted order.
<langsyntaxhighlight lang="jq">[4,3,2,1,1,2,3,4] | unique</langsyntaxhighlight>
 
If all but the first occurrence of each element should be deleted, then the following function could be used. It retains the advantage of imposing no restrictions on the types of elements in the array and for that reason is slightly more complex than would otherwise be required.
<langsyntaxhighlight lang="jq">def removeAllButFirst:
 
# The hash table functions all expect the hash table to be the input.
Line 2,116:
| .[0];
 
</syntaxhighlight>
</lang>
 
=={{header|Julia}}==
{{works with|Julia|0.6}}
 
<langsyntaxhighlight lang="julia">a = [1, 2, 3, 4, 1, 2, 3, 4]
@show unique(a) Set(a)</langsyntaxhighlight>
 
{{out}}
Line 2,131:
(Inspired by the J version.)
 
<langsyntaxhighlight Klang="k"> a:4 5#20?13 / create a random 4 x 5 matrix
(12 7 12 4 3
6 3 7 4 7
Line 2,161:
(0 0 0
0 1 2
0 2 4)</langsyntaxhighlight>
 
=={{header|Klingphix}}==
<syntaxhighlight lang="klingphix">( )
<lang Klingphix>( )
 
( "Now" "is" "the" "time" "for" "all" "good" "men" "to" "come" "to" "the" "aid" "of" "the" "party." )
Line 2,179:
swap print drop nl
 
"End " input</langsyntaxhighlight>
{{out}}
<pre>("Now", "is", "the", "time", "for", "all", "good", "men", "to", "come", "aid", "of", "party.")
Line 2,186:
=={{header|Kotlin}}==
{{trans|Java}}
<langsyntaxhighlight lang="scala">fun main(args: Array<String>) {
val data = listOf(1, 2, 3, "a", "b", "c", 2, 3, 4, "b", "c", "d")
val set = data.distinct()
Line 2,192:
println(data)
println(set)
}</langsyntaxhighlight>
{{out}}
<pre>[1, 2, 3, a, b, c, 2, 3, 4, b, c, d]
Line 2,198:
 
=={{header|Lang5}}==
<langsyntaxhighlight lang="lang5">: dip swap '_ set execute _ ;
 
: remove-duplicates
Line 2,205:
0 extract swap "2dup in if drop else append then" dip ;
 
[1 2 6 3 6 4 5 6] remove-duplicates .</langsyntaxhighlight>
Built-in function:
<langsyntaxhighlight lang="lang5">[1 2 6 3 6 4 5 6] 's distinct
[1 2 6 3 6 4 5 6] 's dress dup union .</langsyntaxhighlight>
 
=={{header|Lasso}}==
<langsyntaxhighlight Lassolang="lasso ">local(
x = array(3,4,8,1,8,1,4,5,6,8,9,6),
y = array
)
with n in #x where #y !>> #n do => { #y->insert(#n) }
// result: array(3, 4, 8, 1, 5, 6, 9)</langsyntaxhighlight>
 
=={{header|Liberty BASIC}}==
LB has arrays, but here the elements are stored in a space-separated string.
<syntaxhighlight lang="lb">
<lang lb>
a$ =" 1 $23.19 2 elbow 3 2 Bork 4 3 elbow 2 $23.19 "
print "Original set of elements = ["; a$; "]"
Line 2,239:
removeDuplicates$ =o$
end function
</syntaxhighlight>
</lang>
Original set of elements = [ 1 $23.19 2 elbow 3 2 Bork 4 3 elbow 2 $23.19 ]
Line 2,246:
=={{header|Logo}}==
{{works with|UCB Logo}}
<langsyntaxhighlight lang="logo">show remdup [1 2 3 a b c 2 3 4 b c d] ; [1 a 2 3 4 b c d]</langsyntaxhighlight>
 
=={{header|Lua}}==
<langsyntaxhighlight Lualang="lua">items = {1,2,3,4,1,2,3,4,"bird","cat","dog","dog","bird"}
flags = {}
io.write('Unique items are:')
Line 2,258:
end
end
io.write('\n')</langsyntaxhighlight>
{{out}}
<pre>Unique items are: 1 2 3 4 bird cat dog</pre>
 
Lua doesn't accept Not-a-Number (NaN) and nil as table key, we can handle them like this (Lua 5.3):
<langsyntaxhighlight Lualang="lua">local items = {1,2,3,4,1,2,3,4,0/0,nil,"bird","cat","dog","dog","bird",0/0}
 
function rmdup(t)
Line 2,277:
end
 
print(table.concat(rmdup(items),' '))</langsyntaxhighlight>
{{out}}
<pre>1 2 3 4 nan bird cat dog</pre>
Line 2,283:
=={{header|Maple}}==
This is simplest with a list, which is an immutable array.
<langsyntaxhighlight Maplelang="maple">> L := [ 1, 2, 1, 2, 3, 3, 2, 1, "a", "b", "b", "a", "c", "b" ];
L := [1, 2, 1, 2, 3, 3, 2, 1, "a", "b", "b", "a", "c", "b"]
 
> [op]({op}(L));
[1, 2, 3, "a", "b", "c"]</langsyntaxhighlight>
That is idiomatic, but perhaps a bit cryptic; here is a more verbose equivalent:
<langsyntaxhighlight Maplelang="maple">> convert( convert( L, 'set' ), 'list' );
[1, 2, 3, "a", "b", "c"]</langsyntaxhighlight>
For an Array, which is mutable, the table solution works well in Maple.
<langsyntaxhighlight Maplelang="maple">> A := Array( L ):
> for u in A do T[u] := 1 end: Array( [indices]( T, 'nolist' ) );
[1, 2, 3, "c", "a", "b"]</langsyntaxhighlight>
Note that the output (due to the Array() constructor) '''is''' in fact an Array.
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
Built-in function:
<langsyntaxhighlight Mathematicalang="mathematica">DeleteDuplicates[{0, 2, 1, 4, 2, 0, 3, 1, 1, 1, 0, 3}]</langsyntaxhighlight>
gives back:
<langsyntaxhighlight Mathematicalang="mathematica">{0, 2, 1, 4, 3}</langsyntaxhighlight>
Delete duplicates and return sorted elements:
<langsyntaxhighlight Mathematicalang="mathematica">Union[{0, 2, 1, 4, 2, 0, 3, 1, 1, 1, 0, 3}]</langsyntaxhighlight>
{{out|gives back:}}
<langsyntaxhighlight Mathematicalang="mathematica">{0, 1, 2, 3, 4}</langsyntaxhighlight>
 
=={{header|MATLAB}}==
MATLAB has a built-in function, "unique(list)", which performs this task.
<br \>Sample Usage:
<langsyntaxhighlight MATLABlang="matlab">>> unique([1 2 6 3 6 4 5 6])
 
ans =
 
1 2 3 4 5 6</langsyntaxhighlight>
 
NOTE: The unique function only works for vectors and not for true arrays.
Line 2,320:
=={{header|Maxima}}==
 
<langsyntaxhighlight lang="maxima">unique([8, 9, 5, 2, 0, 7, 0, 0, 4, 2, 7, 3, 9, 6, 6, 2, 4, 7, 9, 8, 3, 8, 0, 3, 7, 0, 2, 7, 6, 0]);
[0, 2, 3, 4, 5, 6, 7, 8, 9]</langsyntaxhighlight>
 
=={{header|MAXScript}}==
<langsyntaxhighlight lang="maxscript">uniques = #(1, 2, 3, "a", "b", "c", 2, 3, 4, "b", "c", "d")
for i in uniques.count to 1 by -1 do
(
id = findItem uniques uniques[i]
if (id != i) do deleteItem uniques i
)</langsyntaxhighlight>
 
=={{header|Microsoft Small Basic}}==
{{trans|Modula-2}}
<langsyntaxhighlight lang="microsoftsmallbasic">
' Set the data.
dataArray[1] = 1
Line 2,364:
TextWindow.WriteLine(resultArray[resultIndex])
EndFor
</syntaxhighlight>
</lang>
 
=={{header|MiniScript}}==
<langsyntaxhighlight MiniScriptlang="miniscript">items = [1, 2, 3, "a", "b", "c", 2, 3, 4, "b", "c", "d"]
d = {}
for i in items
d.push i
end for
print d.indexes</langsyntaxhighlight>
{{out}}
<pre>["b", 1, "d", 3, "a", 4, "c", 2]</pre>
Line 2,379:
==={{header|mLite}}===
A bit like option 3, except copying each element as encountered, and checking to see if it has already been encountered
<langsyntaxhighlight lang="ocaml">fun mem (x, []) = false
| (x eql a, a :: as) = true
| (x, _ :: as) = mem (x, as)
Line 2,394:
println ` implode ` remdup ` explode "the quick brown fox jumped over the lazy dog";
println ` remdup [1,2,3,4,4,3,2,1, "dog","cat","dog", 1.1, 2.2, 3.3, 1.1];
</syntaxhighlight>
</lang>
{{out}}
<pre>the quickbrownfxjmpdvlazyg
Line 2,401:
=={{header|Modula-2}}==
{{works with|ADW Modula-2|any (Compile with the linker option ''Console Application'').}}
<langsyntaxhighlight lang="modula2">
MODULE RemoveDuplicates;
 
Line 2,451:
END;
END RemoveDuplicates.
</syntaxhighlight>
</lang>
 
=={{header|MUMPS}}==
<p>We'll take advantage of the fact that an array can only have one index of any specific value. Sorting into canonical order is a side effect.
If the indices are strings containing the separator string, they'll be split apart.</p><langsyntaxhighlight MUMPSlang="mumps">REMDUPE(L,S)
;L is the input listing
;S is the separator between entries
Line 2,465:
FOR SET I=$O(Z(I)) QUIT:I="" SET R=$SELECT($L(R)=0:I,1:R_S_I)
KILL Z,I
QUIT R</langsyntaxhighlight>
Example:<pre>USER>W $$REMDUPE^ROSETTA("1,2,3,4,5,2,5,""HELLO"",42,""WORLD""",",")
1,2,3,4,5,42,"HELLO","WORLD"</pre>
Line 2,471:
=={{header|Nanoquery}}==
After executing, the list 'unique' will contain only the unique items.
<langsyntaxhighlight lang="nanoquery">items = {1, 2, 3, "a", "b", "c", 2, 3, 4, "b", "c", "d"}
unique = {}
 
Line 2,478:
unique.append(item)
end
end</langsyntaxhighlight>
 
=={{header|Neko}}==
<syntaxhighlight lang="actionscript">/**
<lang ActionScript>/**
Remove duplicate elements, in Neko
*/
Line 2,504:
var show = function(k, v) $print(v, " ")
$hiter(dedup(original), show)
$print("\n")</langsyntaxhighlight>
 
{{out}}
Line 2,513:
 
=={{header|Nemerle}}==
<langsyntaxhighlight Nemerlelang="nemerle">using System.Console;
 
module RemDups
Line 2,523:
WriteLine(unique);
}
}</langsyntaxhighlight>
 
=={{header|NetRexx}}==
This sample takes advantage of the NetRexx built-in <tt>Rexx</tt> object's indexed string capability (associative arrays). <tt>Rexx</tt> indexed strings act very like hash tables:
<langsyntaxhighlight NetRexxlang="netrexx">/* NetRexx */
options replace format comments java crossref symbols nobinary
 
Line 2,574:
 
return wordlist.space
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,588:
 
=={{header|NewLISP}}==
<langsyntaxhighlight NewLISPlang="newlisp">(unique '(1 2 3 a b c 2 3 4 b c d))</langsyntaxhighlight>
 
=={{header|Nial}}==
<langsyntaxhighlight lang="nial">uniques := [1, 2, 3, 'a', 'b', 'c', 2, 3, 4, 'b', 'c', 'd']
cull uniques
=+-+-+-+-+-+-+-+-+
=|1|2|3|a|b|c|4|d|
=+-+-+-+-+-+-+-+-+</langsyntaxhighlight>
 
Using strand form
<langsyntaxhighlight lang="nial">cull 1 1 2 2 3 3
=1 2 3</langsyntaxhighlight>
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim">import sequtils, algorithm, intsets
 
# Go through the list, and for each element, check the rest of the list to see
Line 2,625:
# Sort the elements and remove consecutive duplicate elements.
sort(items, system.cmp[int]) # O(n log n)
echo filterDup(items) # O(n)</langsyntaxhighlight>
 
=={{header|Objeck}}==
<langsyntaxhighlight lang="objeck">
use Structure;
 
Line 2,650:
}
}
</syntaxhighlight>
</lang>
 
=={{header|Objective-C}}==
<langsyntaxhighlight lang="objc">NSArray *items = [NSArray arrayWithObjects:@"A", @"B", @"C", @"B", @"A", nil];
 
NSSet *unique = [NSSet setWithArray:items];</langsyntaxhighlight>
 
=={{header|OCaml}}==
<langsyntaxhighlight lang="ocaml">let uniq lst =
let unique_set = Hashtbl.create (List.length lst) in
List.iter (fun x -> Hashtbl.replace unique_set x ()) lst;
Line 2,664:
 
let _ =
uniq [1;2;3;2;3;4]</langsyntaxhighlight>
 
Another solution (preserves order of first occurrence):
<langsyntaxhighlight lang="ocaml">let uniq lst =
let seen = Hashtbl.create (List.length lst) in
List.filter (fun x -> let tmp = not (Hashtbl.mem seen x) in
Line 2,674:
 
let _ =
uniq [1;2;3;2;3;4]</langsyntaxhighlight>
 
Solution reversing list order :
<langsyntaxhighlight lang="ocaml">let uniq l =
let rec tail_uniq a l =
match l with
| [] -> a
| hd::tl -> tail_uniq (hd::a) (List.filter (fun x -> x != hd) tl) in
tail_uniq [] l</langsyntaxhighlight>
 
{{works with|OCaml|4.02+}}
<langsyntaxhighlight lang="ocaml">List.sort_uniq compare [1;2;3;2;3;4]</langsyntaxhighlight>
 
=={{header|Octave}}==
<langsyntaxhighlight lang="octave">
input=[1 2 6 4 2 32 5 5 4 3 3 5 1 2 32 4 4];
output=unique(input);
</syntaxhighlight>
</lang>
 
=={{header|Oforth}}==
Line 2,706:
 
=={{header|ooRexx}}==
<langsyntaxhighlight ooRexxlang="oorexx">data = .array~of(1, 2, 3, "a", "b", "c", 2, 3, 4, "b", "c", "d")
uniqueData = .set~new~union(data)~makearray~sort
 
Line 2,713:
do item over uniqueData
say item
end</langsyntaxhighlight>
{{out}}
<pre>Unique elements are
Line 2,729:
The following solutions only works if the value type is allowed as a key in a dictionary.
 
<langsyntaxhighlight lang="oz">declare
 
fun {Nub Xs}
Line 2,740:
in
 
{Show {Nub [1 2 1 3 5 4 3 4 4]}}</langsyntaxhighlight>
 
=={{header|PARI/GP}}==
Sort and remove duplicates. Other methods should be implemented as well.
<langsyntaxhighlight lang="parigp">rd(v)={
vecsort(v,,8)
};</langsyntaxhighlight>
 
=={{header|Pascal}}==
<langsyntaxhighlight lang="pascal">Program RemoveDuplicates;
 
const
Line 2,781:
for i := low(rArray) to last do
writeln (rArray[i]);
end.</langsyntaxhighlight>
{{out}}
<pre>% ./RemoveDuplicates
Line 2,793:
{{libheader|List::MoreUtils}}
(this version even preserves the order of first appearance of each element)
<langsyntaxhighlight lang="perl">use List::MoreUtils qw(uniq);
 
my @uniq = uniq qw(1 2 3 a b c 2 3 4 b c d);</langsyntaxhighlight>
 
It is implemented like this:
<langsyntaxhighlight lang="perl">my %seen;
my @uniq = grep {!$seen{$_}++} qw(1 2 3 a b c 2 3 4 b c d);</langsyntaxhighlight>
 
Note: the following two solutions convert elements to strings in the result, so if you give it references they will lose the ability to be dereferenced.
 
Alternately:
<langsyntaxhighlight lang="perl">my %hash = map { $_ => 1 } qw(1 2 3 a b c 2 3 4 b c d);
my @uniq = keys %hash;</langsyntaxhighlight>
 
Alternately:
<langsyntaxhighlight lang="perl">my %seen;
@seen{qw(1 2 3 a b c 2 3 4 b c d)} = ();
my @uniq = keys %seen;</langsyntaxhighlight>
 
=={{header|Phix}}==
Standard builtin. The "STABLE" option preserves order of first occurence. Applies to any data type. The default option for unique(), "SORT", obviously produces sorted output, and there is one other recognised option, "PRESORTED", which can be used either to avoid an unnecessary sort, or to only remove adjacent duplicates.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">unique</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"Now is the time for all good men to come to the aid of the party."</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"STABLE"</span><span style="color: #0000FF;">))</span>
Line 2,821:
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">unique</span><span style="color: #0000FF;">({</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">9</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">8</span><span style="color: #0000FF;">,</span><span style="color: #000000;">8</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">},</span><span style="color: #008000;">"STABLE"</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">unique</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"chthonic eleemosynary paronomasiac"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"STABLE"</span><span style="color: #0000FF;">)</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 2,832:
 
=={{header|Phixmonti}}==
<langsyntaxhighlight Phixmontilang="phixmonti">"Now" "is" "the" "time" "for" "all" "good" "men" "to" "come" "to" "the" "aid" "of" "the" "party." stklen tolist
 
0 tolist var newlist
Line 2,845:
endfor
 
newlist print drop</langsyntaxhighlight>
 
=={{header|PHP}}==
<langsyntaxhighlight lang="php">$list = array(1, 2, 3, 'a', 'b', 'c', 2, 3, 4, 'b', 'c', 'd');
$unique_list = array_unique($list);</langsyntaxhighlight>
 
=={{header|PicoLisp}}==
There is a built-in function
<langsyntaxhighlight PicoLisplang="picolisp">(uniq (2 4 6 1 2 3 4 5 6 1 3 5))</langsyntaxhighlight>
{{out}}
<pre>-> (2 4 6 1 3 5)</pre>
 
=={{header|PL/I}}==
<langsyntaxhighlight PLlang="pl/Ii">*process mar(1,72);
remdup: Proc options(main);
declare t(20) fixed initial (6, 6, 1, 5, 6, 2, 1, 7,
Line 2,880:
put skip list ('Unique elements are:');
put edit ((t(k) do k = 1 to i)) (skip,20(f(3)));
end;</langsyntaxhighlight>
{{out}}
<pre>Input:
Line 2,889:
=={{header|Pop11}}==
 
<langsyntaxhighlight lang="pop11">;;; Initial array
lvars ar = {1 2 3 2 3 4};
;;; Create a hash table
Line 2,901:
;;; Collect keys into a list
lvars ls = [];
appdata(ht, procedure(x); cons(front(x), ls) -> ls; endprocedure);</langsyntaxhighlight>
 
=={{header|PostScript}}==
{{libheader|initlib}}
<langsyntaxhighlight lang="postscript">
[10 8 8 98 32 2 4 5 10 ] dup length dict begin aload let* currentdict {pop} map end
</syntaxhighlight>
</lang>
 
=={{header|PowerShell}}==
The common array for both approaches:
<langsyntaxhighlight lang="powershell">$data = 1,2,3,1,2,3,4,1</langsyntaxhighlight>
Using a hash table to remove duplicates:
<langsyntaxhighlight lang="powershell">$h = @{}
foreach ($x in $data) {
$h[$x] = 1
}
$h.Keys</langsyntaxhighlight>
Sorting and removing duplicates along the way can be done with the <code>Sort-Object</code> cmdlet.
<langsyntaxhighlight lang="powershell">$data | Sort-Object -Unique</langsyntaxhighlight>
Removing duplicates without sorting can be done with the <code>Select-Object</code> cmdlet.
<langsyntaxhighlight lang="powershell">$data | Select-Object -Unique</langsyntaxhighlight>
 
=={{header|Prolog}}==
<langsyntaxhighlight lang="prolog">uniq(Data,Uniques) :- sort(Data,Uniques).</langsyntaxhighlight>
 
Example usage:
<langsyntaxhighlight lang="prolog">?- uniq([1, 2, 3, 2, 3, 4],Xs).
Xs = [1, 2, 3, 4]</langsyntaxhighlight>
 
 
Because sort/2 is GNU prolog and not ISO here is an ISO compliant version:
<langsyntaxhighlight lang="prolog">member1(X,[H|_]) :- X==H,!.
member1(X,[_|T]) :- member1(X,T).
 
distinct([],[]).
distinct([H|T],C) :- member1(H,T),!, distinct(T,C).
distinct([H|T],[H|C]) :- distinct(T,C).</langsyntaxhighlight>
 
Example usage:
<langsyntaxhighlight lang="prolog">?- distinct([A, A, 1, 2, 3, 2, 3, 4],Xs).
Xs = [A, 1, 2, 3, 4]</langsyntaxhighlight>
 
=={{header|PureBasic}}==
Task solved with the built in Hash Table which are called Maps in PureBasic
<langsyntaxhighlight PureBasiclang="purebasic">NewMap MyElements.s()
 
For i=0 To 9 ;Mark 10 items at random, causing high risk of duplication items.
Line 2,955:
ForEach MyElements()
Debug MyElements()
Next</langsyntaxhighlight>
Output may look like this, e.g. duplicated items are automatically removed as they have the same hash value.
Number 0 is marked
Line 2,964:
=={{header|Python}}==
If all the elements are ''hashable'' (this excludes ''list'', ''dict'', ''set'', and other mutable types), we can use a <tt>set</tt>:
<langsyntaxhighlight lang="python">items = [1, 2, 3, 'a', 'b', 'c', 2, 3, 4, 'b', 'c', 'd']
unique = list(set(items))</langsyntaxhighlight>
 
or if we want to keep the order of the elements
 
<langsyntaxhighlight lang="python">items = [1, 2, 3, 'a', 'b', 'c', 2, 3, 4, 'b', 'c', 'd']
unique = []
helperset = set()
Line 2,975:
if x not in helperset:
unique.append(x)
helperset.add(x)</langsyntaxhighlight>
 
If all the elements are comparable (i.e. <, >=, etc. operators; this works for ''list'', ''dict'', etc. but not for ''complex'' and many other types, including most user-defined types), we can sort and group:
<langsyntaxhighlight lang="python">import itertools
items = [1, 2, 3, 'a', 'b', 'c', 2, 3, 4, 'b', 'c', 'd']
unique = [k for k,g in itertools.groupby(sorted(items))]</langsyntaxhighlight>
 
If both of the above fails, we have to use the brute-force method, which is inefficient:
<langsyntaxhighlight lang="python">items = [1, 2, 3, 'a', 'b', 'c', 2, 3, 4, 'b', 'c', 'd']
unique = []
for x in items:
if x not in unique:
unique.append(x)</langsyntaxhighlight>
 
 
another way of removing duplicate elements from a list, while preserving the order would be to use OrderedDict module like so
<langsyntaxhighlight lang="python">
from collections import OrderedDict as od
 
print(list(od.fromkeys([1, 2, 3, 'a', 'b', 'c', 2, 3, 4, 'b', 'c', 'd']).keys()))
</syntaxhighlight>
</lang>
 
See also http://www.peterbe.com/plog/uniqifiers-benchmark and http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52560
Line 3,003:
 
One way to do this is to require an equality predicate, or perhaps a key function, in addition to a list to be pruned. For example, using itertools.groupby, at the cost of needing a sort and discarding order:
<langsyntaxhighlight lang="python">from itertools import (groupby)
 
 
Line 3,024:
print (
nubByKey(k, xs)
)</langsyntaxhighlight>
{{Out}}
<pre>['apple', 'aPPLE', 'Apple', 'orange', 'ORANGE', 'Orange', 'ampersand']
Line 3,033:
Or alternatively, using an equality predicate with a recursive function which scales less well, but does preserve order:
 
<langsyntaxhighlight lang="python"># nubByEq :: (a -> a -> Bool) -> [a] -> [a]
def nubByEq(eq, xs):
def go(yys, xxs):
Line 3,070:
print (
nubByEq(eq, xs)
)</langsyntaxhighlight>
 
A briefer definition of which might be in terms of ''filter'':
<langsyntaxhighlight lang="python"># nubBy :: (a -> a -> Bool) -> [a] -> [a]
def nubBy(p, xs):
def go(xs):
Line 3,086:
else:
return []
return go(xs)</langsyntaxhighlight>
 
{{Out}}
Line 3,096:
=={{header|Qi}}==
 
<syntaxhighlight lang="qi">
<lang qi>
(define remove-duplicates
[] -> []
Line 3,103:
 
(remove-duplicates [a b a a b b c d e])
</syntaxhighlight>
</lang>
 
=={{header|Quackery}}==
Line 3,114:
a nest of strings sorted in descending order.
 
<langsyntaxhighlight Quackerylang="quackery"> [ dup [] = iff ]else[ done
' sortwith nested
]'[ nested join do
Line 3,124:
drop ] is uniquewith ( [ --> [ )
' [ 1 2 3 5 6 7 8 1 2 3 4 5 6 7 ] uniquewith > echo</langsyntaxhighlight>
 
{{out}}
Line 3,132:
=={{header|R}}==
 
<langsyntaxhighlight lang="r">items <- c(1,2,3,2,4,3,2)
unique (items)</langsyntaxhighlight>
 
=={{header|Racket}}==
 
Using the built-in function
<syntaxhighlight lang="racket">
<lang Racket>
-> (remove-duplicates '(2 1 3 2.0 a 4 5 b 4 3 a 7 1 3 x 2))
'(2 1 3 2.0 a 4 5 b 7 x)
</syntaxhighlight>
</lang>
 
Using a hash-table:
<syntaxhighlight lang="racket">
<lang Racket>
(define (unique/hash lst)
(hash-keys (for/hash ([x (in-list lst)]) (values x #t))))
</syntaxhighlight>
</lang>
 
Using a set:
<syntaxhighlight lang="racket">
<lang Racket>
(define unique/set (compose1 set->list list->set))
</syntaxhighlight>
</lang>
 
A definition that works with arbitrary sequences and allows
specification of an equality predicate.
 
<syntaxhighlight lang="racket">
<lang Racket>
(define (unique seq #:same-test [same? equal?])
(for/fold ([res '()])
([x seq] #:unless (memf (curry same? x) res))
(cons x res)))
</syntaxhighlight>
</lang>
<pre>
-> (unique '(2 1 3 2.0 a 4 5 b 4 3 a 7 1 3 x 2))
Line 3,176:
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" perl6line>my @unique = [1, 2, 3, 5, 2, 4, 3, -3, 7, 5, 6].unique;</langsyntaxhighlight>
Or just make a set of it.
<syntaxhighlight lang="raku" perl6line>set(1,2,3,5,2,4,3,-3,7,5,6).list</langsyntaxhighlight>
 
=={{header|Raven}}==
 
<langsyntaxhighlight lang="raven">[ 1 2 3 'a' 'b' 'c' 2 3 4 'b' 'c' 'd' ] as items
items copy unique print
 
Line 3,193:
5 => "c"
6 => 4
7 => "d"</langsyntaxhighlight>
 
=={{header|REBOL}}==
<langsyntaxhighlight REBOLlang="rebol">print mold unique [1 $23.19 2 elbow 3 2 Bork 4 3 elbow 2 $23.19]</langsyntaxhighlight>
 
{{out}}
Line 3,202:
 
=={{header|Red}}==
<langsyntaxhighlight Redlang="red">>> items: [1 "a" "c" 1 3 4 5 "c" 3 4 5]
>> unique items
== [1 "a" "c" 3 4 5]</langsyntaxhighlight>
 
=={{header|REXX}}==
Line 3,218:
 
===version 1, using method 1===
<langsyntaxhighlight lang="rexx">/*REXX program removes any duplicate elements (items) that are in a list (using a hash).*/
$= '2 3 5 7 11 13 17 19 cats 222 -100.2 +11 1.1 +7 7. 7 5 5 3 2 0 4.4 2' /*item list.*/
say 'original list:' $
Line 3,228:
say
say 'modified list:' space(z) /*stick a fork in it, we're all done. */
say right( words(z), 17, '─') 'words in the modified list.'</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input list:}}
<pre>
Line 3,242:
 
Sorting of the list elements isn't necessary.
<langsyntaxhighlight lang="rexx">/*REXX program removes any duplicate elements (items) that are in a list (using a list).*/
$= '2 3 5 7 11 13 17 19 cats 222 -100.2 +11 1.1 +7 7. 7 5 5 3 2 0 4.4 2' /*item list.*/
say 'original list:' $
Line 3,252:
say
say 'modified list:' space($) /*stick a fork in it, we're all done. */
say right( words(z), 17, '─') 'words in the modified list.'</langsyntaxhighlight>
{{out|output|text=&nbsp; is identical to the 1<sup>st</sup> REXX version.}} <br><br>
 
===version 3, using method 3===
<langsyntaxhighlight lang="rexx">/*REXX program removes any duplicate elements (items) that are in a list (using 2 lists)*/
old = '2 3 5 7 11 13 17 19 cats 222 -100.2 +11 1.1 +7 7. 7 5 5 3 2 0 4.4 2'
say 'original list:' old
Line 3,266:
say
say 'modified list:' space(new) /*stick a fork in it, we're all done. */
say right( words(new), 17, '─') 'words in the modified list.'</langsyntaxhighlight>
{{out|output|text=&nbsp; is identical to the 1<sup>st</sup> REXX version.}} <br><br>
 
===version 4, using method 1 (hash table) via REXX stems===
<langsyntaxhighlight lang="rexx">/* REXX ************************************************************
* 26.11.2012 Walter Pachl
* added: show multiple occurrences
Line 3,297:
If count.w>1 Then
Say right(count.w,3) w
End</langsyntaxhighlight>
{{out}}
<pre>
Line 3,313:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
list = ["Now", "is", "the", "time", "for", "all", "good", "men", "to", "come", "to", "the", "aid", "of", "the", "party."]
for i = 1 to len(list)
Line 3,325:
next
see nl
</syntaxhighlight>
</lang>
Output:
<pre>
Line 3,333:
=={{header|Ruby}}==
Ruby has an <code>Array#uniq</code> built-in method, which returns a new array by removing duplicate values in self.
<langsyntaxhighlight lang="ruby">ary = [1,1,2,1,'redundant',[1,2,3],[1,2,3],'redundant']
p ary.uniq # => [1, 2, "redundant", [1, 2, 3]]</langsyntaxhighlight>
 
You can also write your own uniq method.
<langsyntaxhighlight lang="ruby">class Array
# used Hash
def uniq1
Line 3,362:
p ary.uniq1 #=> [1, 2, 3, 7, 6, 5, 4]
p ary.uniq2 #=> [1, 2, 3, 4, 5, 6, 7]
p ary.uniq3 #=> [1, 2, 3, 7, 6, 5, 4]</langsyntaxhighlight>
 
A version without implementing class declarations:
<langsyntaxhighlight lang="ruby">def unique(array)
pure = Array.new
for i in array
Line 3,378:
 
unique ["hi","hey","hello","hi","hey","heyo"] # => ["hi", "hey", "hello", "heyo"]
unique [1,2,3,4,1,2,3,5,1,2,3,4,5] # => [1,2,3,4,5]</langsyntaxhighlight>
 
=={{header|Run BASIC}}==
<langsyntaxhighlight lang="runbasic">a$ = "2 3 5 7 11 13 17 19 cats 222 -100.2 +11 1.1 +7 7. 7 5 5 3 2 0 4.4 2"
 
for i = 1 to len(a$)
Line 3,394:
print "Dups:";a$
print "No Dups:";b$</langsyntaxhighlight>
<pre>Dups:2 3 5 7 11 13 17 19 cats 222 -100.2 +11 1.1 +7 7. 7 5 5 3 2 0 4.4 2
No Dups:2 3 5 7 11 13 17 19 cats 222 -100.2 +11 1.1 +7 7. 0 4.4 </pre>
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">use std::collections::HashSet;
use std::hash::Hash;
 
Line 3,417:
remove_duplicate_elements_sorting(&mut sample_elements);
println!("After removal of duplicates : {:?}", sample_elements);
}</langsyntaxhighlight>
{{out}}
<pre>
Line 3,425:
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">val list = List(1,2,3,4,2,3,4,99)
val l2 = list.distinct
// l2: scala.List[scala.Int] = List(1,2,3,4,99)
Line 3,432:
val arr2 = arr.distinct
// arr2: Array[Int] = Array(1, 2, 3, 4, 99)
</syntaxhighlight>
</lang>
 
=={{header|Scheme}}==
<langsyntaxhighlight lang="scheme">(define (remove-duplicates l)
(cond ((null? l)
'())
Line 3,443:
(cons (car l) (remove-duplicates (cdr l))))))
 
(remove-duplicates '(1 2 1 3 2 4 5))</langsyntaxhighlight>
 
<langsyntaxhighlight lang="scheme">(1 3 2 4 5)</langsyntaxhighlight>
 
Alternative approach:
<langsyntaxhighlight lang="scheme">(define (remove-duplicates l)
(do ((a '() (if (member (car l) a) a (cons (car l) a)))
(l l (cdr l)))
((null? l) (reverse a))))
 
(remove-duplicates '(1 2 1 3 2 4 5))</langsyntaxhighlight>
 
<langsyntaxhighlight lang="scheme">(1 2 3 4 5)</langsyntaxhighlight>
 
The function 'delete-duplicates' is also available in srfi-1.
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
const proc: main is func
Line 3,472:
end for;
writeln(dataSet);
end func;</langsyntaxhighlight>
 
{{out}}
Line 3,480:
 
=={{header|SETL}}==
<langsyntaxhighlight SETLlang="setl">items := [0,7,6,6,4,9,7,1,2,3,2];
print(unique(items));</langsyntaxhighlight>
Output in arbitrary order (convert tuple->set then set->tuple):
<langsyntaxhighlight SETLlang="setl">proc unique(items);
return [item: item in {item: item in items}];
end proc;</langsyntaxhighlight>
 
Preserving source order
<langsyntaxhighlight SETLlang="setl">proc unique(items);
seen := {};
return [item: item in items, nps in {#seen} | #(seen with:= item) > nps];
end proc;</langsyntaxhighlight>
 
 
<langsyntaxhighlight SETLlang="setl">proc unique(items);
seen := {};
return [item: item in items, nps in {#seen} | #(seen with:= item) > nps];
end proc;</langsyntaxhighlight>
<langsyntaxhighlight SETLlang="setl">items := [0,7,6,6,4,9,7,1,2,3,2];
print(unique(items));</langsyntaxhighlight>
Output in arbitrary order (convert tuple->set then set->tuple):
<langsyntaxhighlight SETLlang="setl">proc unique(items);
return [item: item in {item: item in items}];
end proc;</langsyntaxhighlight>
 
=={{header|SETL4}}==
 
<syntaxhighlight lang="setl4">
<lang SETL4>
set = new('set')
* Add all the elements of the array to the set.
add(set,array)
</syntaxhighlight>
</lang>
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">var ary = [1,1,2,1,'redundant',[1,2,3],[1,2,3],'redundant'];
say ary.uniq.dump;
say ary.last_uniq.dump;</langsyntaxhighlight>
{{out}}
<pre>[1, 2, 'redundant', [1, 2, 3]]
Line 3,523:
=={{header|Slate}}==
 
<langsyntaxhighlight lang="slate">[|:s| #(1 2 3 4 1 2 3 4) >> s] writingAs: Set.
 
"==> {"Set traitsWindow" 1. 2. 3. 4}"</langsyntaxhighlight>
 
=={{header|Smalltalk}}==
 
<langsyntaxhighlight lang="smalltalk">"Example of creating a collection"
|a|
a := #( 1 1 2 'hello' 'world' #symbol #another 2 'hello' #symbol ).
a asSet.</langsyntaxhighlight>
{{out}}
<pre>Set (1 2 #symbol 'world' #another 'hello' )</pre>
Line 3,541:
on my system. This can be avoided by using an ordered set (which has also O(n) complexity) as below:
{{works with|Smalltalk/X}}
<langsyntaxhighlight lang="smalltalk">|a|
a := #( 1 1 2 'hello' 'world' #symbol #another 2 'hello' #symbol ).
a asOrderedSet.</langsyntaxhighlight>
{{out}}
<pre>OrderedSet(1 2 'hello' 'world' #symbol #another)</pre>
 
=={{header|Sparkling}}==
<langsyntaxhighlight lang="sparkling">function undupe(arr) {
var t = {};
foreach(arr, function(key, val) {
Line 3,560:
 
return r;
}</langsyntaxhighlight>
 
=={{header|SQL}}==
Line 3,566:
This is not a particularly efficient solution, but it gets the job done.
 
<syntaxhighlight lang="sql">
<lang SQL>
/*
This code is an implementation of "Remove duplicate elements" in SQL ORACLE 19c
Line 3,590:
select remove_duplicate_elements('3 9 1 10 3 7 6 5 2 7 4 7 4 2 2 2 2 8 2 10 4 9 2 4 9 3 4 3 4 7',' ') as res from dual
;
</syntaxhighlight>
</lang>
 
{{out}}
Line 3,602:
Stata can report duplicate lines, or remove them. See '''[http://www.stata.com/help.cgi?duplicates duplicates]''' in Stata help.
 
<langsyntaxhighlight lang="stata">. clear all
. input x y
1 1
Line 3,626:
3. | 2 1 |
4. | 2 2 |
+-------+</langsyntaxhighlight>
 
===Mata===
Line 3,632:
The '''[http://www.stata.com/help.cgi?mf_uniqrows uniqrows]''' function removes duplicate rows from a matrix.
 
<langsyntaxhighlight lang="stata">. mata
: a=1,1\1,1\1,2\2,1\2,2\1,1\2,1\2,1\1,2\2,2
 
Line 3,657:
3 | 2 1 |
4 | 2 2 |
+---------+</langsyntaxhighlight>
 
=={{header|Swift}}==
Requires elements to be hashable:
{{works with|Swift|1.2+}}
<langsyntaxhighlight lang="swift">println(Array(Set([3,2,1,2,3,4])))</langsyntaxhighlight>
{{out}}
<pre>[2, 3, 1, 4]</pre>
Line 3,668:
Another solution (preserves order of first occurrence). Also requires elements to be hashable:
{{works with|Swift|1.2+}}
<langsyntaxhighlight lang="swift">func uniq<T: Hashable>(lst: [T]) -> [T] {
var seen = Set<T>(minimumCapacity: lst.count)
return lst.filter { x in
Line 3,677:
}
 
println(uniq([3,2,1,2,3,4]))</langsyntaxhighlight>
{{out}}
<pre>[3, 2, 1, 4]</pre>
 
Only requires elements to be equatable, but runs in O(n^2):
<langsyntaxhighlight lang="swift">func uniq<T: Equatable>(lst: [T]) -> [T] {
var seen = [T]()
return lst.filter { x in
Line 3,693:
}
 
println(uniq([3,2,1,2,3,4]))</langsyntaxhighlight>
{{out}}
<pre>[3, 2, 1, 4]</pre>
Line 3,701:
What is called "array" in many other languages is probably better represented by the "list" in Tcl (as in LISP).
With the correct option, the <code>lsort</code> command will remove duplicates.
<langsyntaxhighlight lang="tcl">set result [lsort -unique $listname]</langsyntaxhighlight>
 
 
Line 3,707:
{{trans|GW-BASIC}}
{{works with|QBasic}}
<langsyntaxhighlight lang="basic">
OPTION BASE 1
LET max = 10
Line 3,738:
NEXT i
END
</syntaxhighlight>
</lang>
 
=={{header|TSE SAL}}==
<syntaxhighlight lang="tsesal">
<lang TSESAL>
//
// Go through the list, and for each element, check the rest of the list to see if it appears again, and discard it if it does.
Line 3,779:
GotoBufferId( bufferI )
END
</syntaxhighlight>
</lang>
{{out}} <pre>
Input
Line 3,803:
 
=={{header|TUSCRIPT}}==
<langsyntaxhighlight lang="tuscript">
$$ MODE TUSCRIPT
list_old="b'A'A'5'1'2'3'2'3'4"
Line 3,810:
PRINT list_old
PRINT list_new
</syntaxhighlight>
</lang>
{{out}} (sorted)
<pre>
Line 3,817:
</pre>
or
<langsyntaxhighlight lang="tuscript">
$$ MODE TUSCRIPT
list_old="b'A'A'5'1'2'3'2'3'4"
Line 3,829:
PRINT list_old
PRINT list_new
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,838:
=={{header|UnixPipes}}==
Assuming a sequence is represented by lines in a file.
<langsyntaxhighlight lang="bash">bash$ # original list
bash$ printf '6\n2\n3\n6\n4\n2\n'
6
Line 3,852:
4
6
bash$</langsyntaxhighlight>
 
or
 
<langsyntaxhighlight lang="bash">bash$ # made uniq
bash$ printf '6\n2\n3\n6\n4\n2\n'|sort -nu
2
Line 3,862:
4
6
bash$</langsyntaxhighlight>
 
=={{header|Ursala}}==
Line 3,872:
to decide whether that's a relevant criterion for equivalence or else
specify a better one.
<langsyntaxhighlight Ursalalang="ursala">#cast %s
 
example = |=hS& 'mississippi'</langsyntaxhighlight>
{{out}}
<pre>'mspi'</pre>
Line 3,882:
Input list (variant : Long, Double, Boolean and Strings) :
Array(1.23456789101112E+16, True, False, True, "Alpha", 1, 235, 4, 1.25, 1.25, "Beta", 1.23456789101112E+16, "Delta", "Alpha", "Charlie", 1, 2, "Foxtrot", "Foxtrot", "Alpha", 235)
<syntaxhighlight lang="vb">
<lang vb>
Option Explicit
 
Line 3,911:
ReDim Preserve Temp(cpt - 1)
Remove_Duplicate = Temp
End Function</langsyntaxhighlight>
{{out}}
<pre> 1.23456789101112E+16
Line 3,929:
=={{header|VBScript}}==
Hash Table Approach
<syntaxhighlight lang="vb">
<lang vb>
Function remove_duplicates(list)
arr = Split(list,",")
Line 3,945:
 
WScript.Echo remove_duplicates("a,a,b,b,c,d,e,d,f,f,f,g,h")
</syntaxhighlight>
</lang>
 
{{Out}}
Line 3,953:
 
The input "array" is an edit buffer where each line is one element.
<langsyntaxhighlight lang="vedit">Sort(0, File_Size) // sort the data
While(Replace("^(.*)\N\1$", "\1", REGEXP+BEGIN+NOERR)){} // remove duplicates</langsyntaxhighlight>
 
=={{header|Vim Script}}==
 
<langsyntaxhighlight lang="vim">call filter(list, 'count(list, v:val) == 1')</langsyntaxhighlight>
 
=={{header|Visual FoxPro}}==
<langsyntaxhighlight lang="vfp">
LOCAL i As Integer, n As Integer, lcOut As String
CLOSE DATABASES ALL
Line 3,986:
RETURN INT((tnHigh - tnLow + 1)*RAND() + tnLow)
ENDFUNC
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 4,004:
 
=={{header|Vlang}}==
<langsyntaxhighlight lang="vlang">const list = 'a,1,a,b,b,c,d,e,d,0,f,f,5,f,g,h,1,3,3'
 
fn main() {
Line 4,019:
new_list.sort()
return new_list
}</langsyntaxhighlight>
 
{{out}}
Line 4,027:
 
=={{header|Wart}}==
<langsyntaxhighlight lang="python">def (dedup l)
let exists (table)
collect+each x l
unless exists.x
yield x
exists.x <- 1</langsyntaxhighlight>
 
{{out}}
Line 4,039:
 
=={{header|Wortel}}==
<langsyntaxhighlight lang="wortel">@uniq [1 2 3 2 1 2 3] ; returns [1 2 3]</langsyntaxhighlight>
 
=={{header|Wren}}==
{{libheader|Wren-sort}}
<langsyntaxhighlight lang="ecmascript">import "/sort" for Sort
 
// Using a map - order of distinct items is undefined.
Line 4,086:
System.print("Method 1: %(removeDuplicates1.call(a.toList))") // copy original each time
System.print("Method 2: %(removeDuplicates2.call(a.toList))")
System.print("Method 3: %(removeDuplicates3.call(a.toList))")</langsyntaxhighlight>
 
{{out}}
Line 4,097:
 
=={{header|XBS}}==
<langsyntaxhighlight XBSlang="xbs">func RemoveDuplicates(Array){
set NewArray = [];
foreach(k,v as Array){
Line 4,111:
log(Arr);
RemoveDuplicates(Arr);
log(Arr);</langsyntaxhighlight>
{{out}}
<pre>
Line 4,119:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">code Text=12; \built-in routine to display a string of characters
string 0; \use zero-terminated strings (not MSb terminated)
 
Line 4,142:
];
 
Text(0, Unique("Pack my box with five dozen liquor jugs."))</langsyntaxhighlight>
 
{{out}}
Line 4,150:
 
=={{header|Yabasic}}==
<langsyntaxhighlight Yabasiclang="yabasic">data "Now", "is", "the", "time", "for", "all", "good", "men", "to", "come", "to", "the", "aid", "of", "the", "party.", ""
 
do
Line 4,158:
loop
 
print r$</langsyntaxhighlight>
 
=={{header|zkl}}==
Using built ins:
<langsyntaxhighlight lang="zkl">zkl: Utils.Helpers.listUnique(T(1,3,2,9,1,2,3,8,8,"8",1,0,2,"8"))
L(1,3,2,9,8,"8",0)
zkl: "1,3,2,9,1,2,3,8,8,1,0,2".unique()
,012389</langsyntaxhighlight>
Where listUnique is brute force:
<langsyntaxhighlight lang="zkl">fcn listUnique(xs){
xs.reduce(fcn(us,s){us.holds(s) and us or us.append(s)},L()) }</langsyntaxhighlight>
 
=={{header|Zoea}}==
<syntaxhighlight lang="zoea">
<lang Zoea>
program: remove_duplicate_elements
input: [1,2,1,3,2,4,1]
output: [1,2,3,4]
</syntaxhighlight>
</lang>
 
=={{header|Zoea Visual}}==
10,333

edits