Laguna Beach Live! Is Great Talent... Intimate... Accessible...


Join date: Sep 27, 2022


What is a unique value in an array?

Finding unique values in an array is a common task for developers. Fortunately, there are several ways to do it: some require writing code, some use simple arrays and some use methods provided by the JavaScript language. A unique value in an array in JavaScript is a value that appears exactly once. If you have an array with three integers, a unique value would be one of those three integers.

javascript find unique values in the array

// usage example:

var myArray = ['a', 1, 'a', 2, '1'];

var unique = myArray.filter((v, i, a) => a.indexOf(v) === i);

// unique is ['a', 1, 2, '1']


In JavaScript, a unique value for an array is one that cannot be duplicated. This means that if you have two objects containing identical properties, then you will get two different values.

const myArray = [1,2,3,1,5,8,1,2,9,4];

const unique = [ Set(myArray)]; // [1, 2, 3, 5, 8, 9, 4]

const myString = ["a","b","c","a","d","b"];

const uniqueString = [ Set(myString)]; //["a", "b", "c", "d"]

The unique values are the only ones that can be put in the array. So if you have an array of numbers, a unique value could be anyone from 1 to 100, except 99.

 Partially funded by the lodging establishments and City of Laguna Beach

P. O. Box 9336  -  Laguna Beach, CA 92652

© 2023 by Laguna Beach Live, Inc. a Nonprofit 501c3 Corporation

  • Facebook
  • Twitter
  • Instagram

Unique Value in an Array

More actions