Checking Whether Javascript Array Numbers are Unique

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

Founder of Firefly Semantics Corporation

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Automate Your Deployment Process with PM2

DIY React Popups with Popper.js 2

Rendering 10 Million Items with {{vertical-collection}} and Ember.js

Connect Firebase Realtime NoSQL Cloud Database with Angular Project

The Ultimate Beginner’s Guide to Learn node.js

Authentication made easy in Vue.js with Keycloak

Flutter > React Native

User login and logout with Google Auth in a React application

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ole Ersoy

Ole Ersoy

Founder of Firefly Semantics Corporation

More from Medium

Formatting Javascript Strings and Numbers to 2 Decimal Places

call(), apply() & bind() methods in Javascript

Understanding hoisting,scopes, and closures in JavaScript

Array methods in JavaScript that you should know