Count The Unique Elements In An Array In JavaScript

To count the unique elements in an array in JavaScript, you can use any of the following methods –

  1. Pass the array to the Set constructor and use the size() method – This will give you the count of unique elements in the array.
  2. Use a forEach() method and call indexOf() inside the callback function – This will give you the count of unique elements in the array.
  3. Use a reduce() method and call indexOf() inside the reduce callback function – This will give you the count of unique elements in the array.
  4. Use the filter() method and return an array with unique elements – This will give you the count of unique elements in the array.

Let’s discuss each of these methods in detail below.

Count The Unique Elements In An Array In JavaScript

count unique elements in an array in javascript

Method 1: Pass The Array To The Set Constructor And Use The size() Method

The Set object lets you store unique values of any type, whether primitive values or object references.

You can pass the array to the Set constructor to remove duplicate elements from it.

After that, you can use the size property to get the count of unique elements in the set.

See the following code.

const arr = [‘a’, ‘b’, ‘c’, ‘d’, ‘a’, ‘e’, ‘f’, ‘c’];

const unique = new Set(arr);

console.log(‘Unique elements in the array are ‘+ unique);

console.log(‘Count of unique elements is ‘ + unique.size);

Output:

Unique elements in the array are Set(6) {“a”, “b”, “c”, “d”, “e”, “f”}

Count of unique elements are 6.

Method 2: Use A forEach() Method And Call indexOf() Inside The Callback Function

Another way to find the unique elements in an array is to use the forEach() method and call the indexOf() method inside its callback function.

For every element, check if it exists only once in the given array by calling indexOf().

If so, increase the count variable. At last, print the count variable.

const arr = [‘a’, ‘b’, ‘c’, ‘d’, ‘a’, ‘e’, ‘f’, ‘c’];

let count = 0;

arr.forEach(function (element) {

  if (arr.indexOf(element) === arr.lastIndexOf(element)) {

    count++;

  }

});

console.log(‘Count of unique elements is ‘ + count);

Output:

Count of unique elements is 4

Method 3: Use A reduce() Method And Call indexOf() Inside The reduce Callback Function

You can also use the reduce() method to find unique elements in an array.

This is similar to the forEach() method with a difference that it uses a reducer function.

The reducer function takes two parameters – accumulator (acc) and current value (curr).

In the reducer function, check if the current value exists only once in the given array by calling indexOf().

If so, increase the count variable. At last, print the count variable.

const arr = [‘a’, ‘b’, ‘c’, ‘d’, ‘a’, ‘e’, ‘f’, ‘c’];

const count = arr.reduce(function (acc, curr) {

if (arr.indexOf(curr) === arr.lastIndexOf(curr)) {

  return acc + 1;

} else {

  return acc;

}

}, 0);

console.log(‘Count of unique elements is ‘ + count);

Output:

Count of unique elements is 4

Method 4: Use The filter() Method And Return An Array With Unique Elements

You can also use the filter() method to count the unique elements in an array in JavaScript.

The filter() method returns a new array with only those elements from the given array for which the callback function returns a truthy value.

In the callback function, check if the current element exists only once in the given array by calling indexOf().

If so, return it. At last, print the length of the returned array.

const arr = [‘a’, ‘b’, ‘c’, ‘d’, ‘a’, ‘e’, ‘f’, ‘c’];

const unique = arr.filter(function (element) {

   return arr.indexOf(element) === arr.lastIndexOf(element);

});

console.log(‘Count of unique elements is ‘ + unique.length);

Output:

Count of unique elements is 4

Conclusion

In this article, you learned four different ways to count the unique elements in an array in JavaScript.

You can choose any method depending on your requirements.

I hope this article was helpful to you. Thank you for reading!

Leave a Reply