Reference · Lists

unique

signature
unique(list | null) -> list

What it does

The list without repeats: the first of each group of equal items, in the order they first appear. Equal is what == says, so 1, 1.0 and dec("1.0") are one item, and a map or a list equals another with the same contents. null counts as an empty list.

Examples

print(unique([1, 2, 1, 3, 2]))print(unique(["ann", "bob", "ann"]))let orders = [{ user: "ann" }, { user: "bob" }, { user: "ann" }]print(unique(map(orders, (o) => o.user)))
output
[1, 2, 3]["ann", "bob"]["ann", "bob"]

Edge cases

print(unique([]), unique(null))print(unique([1, 1.0, dec("1.00"), "1"]))print(unique(["a", "A", "a"]), unique([null, null, 1]))print(unique([[1, 2], [1, 2], { a: 1 }, { a: 1 }]))print(unique([9007199254740993, 9007199254740992.0, 9007199254740992]))
output
[] [][1, "1"]["a", "A"] [null, 1][[1, 2], { a: 1 }][9007199254740993, 9007199254740992.0]

Errors

An argument that is not a list or null is a runtime error, not an error value; split a string first to get its distinct characters.

this one fails
print(unique("aab"))
output
line 1: `unique` expects a list as argument 1, got str

Chaining

In a pipeline unique takes the list and needs no arguments. Sort after it for an ordered set, count it for the number of distinct values.

print([3, 1, 3, 2] | unique | sort)print(["b", "a", "b"] | unique | len)print(split("hello", "") | unique | join(""))
output
[1, 2, 3]2helo

See also

filter, sort, group_by, contains, count

Thread
8Useful? Like it, or add to its thread below.

Thread 0 posts

On unique(): questions, tips, corrections, and code that uses it.

No posts yet. The first one opens the thread.

    Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo