Checking Whether Javascript Array Numbers are Unique

Ole Ersoy
Oct 23, 2021

--

Image by Steffen Wachsmuth from Pixabay

Scenario

We have an array [1,2,3] and we want to know whether the numbers are unique.

Approach

function isArrayUnique(target: any[]): boolean {
const uniqueItems = target.filter((a, b, c) => c.indexOf(a) === b);
return target.length === uniqueItems.length;
}

Demo

--

--