Count Unique Elements in Array Without Sorting using JavaScript
Last Updated :
15 May, 2024
One can count unique elements(distinct elements) present in an array without sorting JavaScript. There are several methods of counting unique elements without sorting in JavaScript. Below is an example to understand the problem clearly.
Example:
Input: [ 1,2, 3, 1, 3, 4, 5, 5, 2]
Output: 5
Explanation: Unique Elements in array : 1, 2, 3, 4, 5. Total 5 unique elements present in array
There are several approaches to count unique elements in array without sorting in JavaScript which are as follows:
Using Array Iteration
Use the forEach method to iterate through each element of the input array and for each element in the input array, check if it exists in the unique array using the includes method. If the element is not already present in the unique array, push it into the unique array. Return the length of the unique array.
Example: To demonstrate the counting of unique elements in array using Array Iteration in JavaScript.
JavaScript
function countUniqueArray(arr) {
const unique = [];
arr.forEach(num => {
if (!unique.includes(num)) {
unique.push(num);
}
});
return unique.length;
}
const arr = [1, 2, 3, 1, 4, 2, 5];
console.log(countUniqueArray(arr));
Time complexity: O(n2)
Space complexity: O(n)
Using a Set
Initialize a new Set. Use the Set's constructor to create a Set from the input array arr. This will automatically remove duplicates as Sets only store unique elements. Return the size of set.
Example: To demonstrate the counting of unique elements in array using a set method in JavaScript.
JavaScript
function countUniqueSet(arr) {
// Create a Set to s
// tore unique elements
const uniqueSet = new Set(arr);
// Return the size of the Set,
// which represents the count of unique elements
return uniqueSet.size;
}
const arr = [1, 2, 3, 1, 4, 2, 5];
console.log(countUniqueSet(arr));
Time complexity: O(n)
Space complexity: O(n)
Using an object/Map
Create an empty object. Use the forEach method to iterate through each element of the input array. For each element in the input array, set its value in the map object to true. Since objects in JavaScript can only have unique keys, this effectively stores each unique element of the array as a key in the object. Use Object.keys(map) to get an array of all the keys in the map object, and then return the length of this array.
Example: To demonstrate the counting of unique elements in array using an object/Map in JavaScript.
JavaScript
function countUniqueMap(arr) {
// Initialize an empty object
// to store unique elements
const map = {};
// Iterate through the input array
arr.forEach(num => map[num] = true);
// Return the number of keys in the object,
// which represents the count of unique elements
return Object.keys(map).length;
}
const arr = [1, 2, 3, 1, 4, 2, 5];
console.log(countUniqueMap(arr));
Time complexity: O(n)
Space complexity: O(n)
Similar Reads
JavaScript - Print Unique Elements From Two Unsorted Arrays Here are the different approaches to print unique elements from two unsorted arrays using JavaScript1. Using filter() methodWe can filter all the unique elements using the filter() method. Then we will make one new array to concat our filtered array.JavaScriptlet a1 = [54, 71, 58, 95, 20]; let a2 =
2 min read
How to calculate the XOR of array elements using JavaScript ? In this article, we are given an array, and the task is to find the XOR of Array elements using JavaScript. There are two approaches to solve this problem which are discussed below: Method 1: It uses a simple method to access the array elements by an index number and use the loop to find the XOR of
2 min read
How to create an array containing non-repeating elements in JavaScript ? In this article, we will learn how to create an array containing non-repeating elements in JavaScript.The following are the two approaches to generate an array containing n number of non-repeating random numbers.Table of ContentUsing do-while loop and includes() MethodUsing a set and checking its si
2 min read
JavaScript - Counting Frequencies of Array Elements Here are the various approaches to count the frequencies of array elements in JavaScript.Using an Object (Simple and Efficient)This is the most common approach for counting frequency in an array. Each array element becomes a key in the object, and its value is incremented as the element appears.Java
2 min read
Counting Sort Visualization using JavaScript GUI(Graphical User Interface) helps in better in understanding than programs. In this article, we will visualize Counting Sort using JavaScript. We will see how the frequencies of elements are stored and how we get the final sorted array. We will also visualize the time complexity of Counting Sort.
4 min read
Javascript Program to Check Majority Element in a sorted array Question: Write a function to find if a given integer x appears more than n/2 times in a sorted array of n integers. Basically, we need to write a function say isMajority() that takes an array (arr[] ), arrayâs size (n) and a number to be searched (x) as parameters and returns true if x is a majorit
3 min read