CyrefJS
CCyrefJS
فا
Back to documentation
Array/Since 1.38.0

unique

Returns a new array containing only the unique elements from the input array.

unique(array):T[]

Parameters

arrayT[]required

The array whose duplicate elements should be removed.

Returns

T[]

A new array containing only unique elements from the input array.

Examples

01

Basic usage

Remove duplicate numbers from an array.

TypeScript
import { unique } from "@cyref/js";

const numbers = [1, 2, 2, 3, 3, 4, 5, 5];

const result = unique(numbers);

console.log(result);
// [1, 2, 3, 4, 5]
02

With strings

Duplicate string values are removed while the first occurrence of each value is preserved.

TypeScript
import { unique } from "@cyref/js";

const languages = [
  "JavaScript",
  "TypeScript",
  "JavaScript",
  "Python",
  "TypeScript",
];

const result = unique(languages);

console.log(result);
// ["JavaScript", "TypeScript", "Python"]
03

Already unique

An array containing only unique elements produces an equivalent new array.

TypeScript
import { unique } from "@cyref/js";

const numbers = [1, 2, 3, 4];

const result = unique(numbers);

console.log(result);
// [1, 2, 3, 4]
04

Empty array

An empty array returns a new empty array.

TypeScript
import { unique } from "@cyref/js";

const result = unique([]);

console.log(result);
// []
Built with@cyref/js·Official documentation