Dealing with duplicate elements in an array is a common task in JavaScript. Whether you’re cleaning up data or processing user input, you may often need to remove duplicate values from an array. Let’s explore some of the approaches to remove duplicate values from Array!
Using set()
A Set
is a built-in JavaScript data structure that automatically removes duplicates from the values it contains.
function removeDuplicates(arr) {
return [...new Set(arr)];
}
// Example usage
const numbers = [1, 2, 3, 2, 4, 1, 5, 6, 3];
const uniqueNumbers = removeDuplicates(numbers);
console.log(uniqueNumbers); // Output: [1, 2, 3, 4, 5, 6]
Using filter()
The filter() method creates a new array filled with elements that pass a test provided by a function.
function removeDuplicates(arr) {
return arr.filter((value, index, item) =>
item.indexOf(value) === index
);
}
const numbers = [1, 2, 3, 2, 4, 1, 5, 6, 3];
const uniqueNumbers = removeDuplicates(numbers);
console.log(uniqueNumbers); // Output: [1, 2, 3, 4, 5, 6]
Using reduce()
The reduce() method executes a reducer function for array element. The reduce() method returns a single value: the function’s accumulated result.
function removeDuplicates(arr) {
return arr.reduce((unique, item) =>
unique.includes(item) ? unique : [...unique, item], []);
}
const numbers = [1, 2, 3, 2, 4, 1, 5, 6, 3];
const uniqueNumbers = removeDuplicates(numbers);
console.log(uniqueNumbers); // Output: [1, 2, 3, 4, 5, 6]
Similar Reads
- JavaScript – Sum of Digits of a NumberIn this article, we will learn how to find the sum of digits of a given number. The sum of digits can be obtained by… Read more: JavaScript – Sum of Digits of a Number
- JavaScript – Reverse a StringIn this article, we’ll look at three basic ways to reverse a string in JavaScript: the built-in reverse() method, a for loop, and the spread operator + reverse(). Using… Read more: JavaScript – Reverse a String
- JavaScript – Find the Intersection of Two ArraysIn this article, we will explore how to implement a function in JavaScript to find the intersection (Common Elements) of two arrays. The problem Write… Read more: JavaScript – Find the Intersection of Two Arrays
- JavaScript – Convert an array to an objectThere are many ways to convert an array to an object in JavaScript. Let’s explore some of the common approaches – Using reduce() In the… Read more: JavaScript – Convert an array to an object
- JavaScript – Shuffle an array in a random orderTo shuffle an array in random order in JavaScript, you can use the sort() function with Math.random() or use the map() & sort() function. Let’s… Read more: JavaScript – Shuffle an array in a random order
- JavaScript – Add elements to a JSON ArrayIn JavaScript, you can add elements to a JSON array in several ways. Here are some common methods : Using push method The push() method in JavaScript… Read more: JavaScript – Add elements to a JSON Array
- JavaScript – Remove specific property from an objectTo remove a specific property from an object in JavaScript, you can use the delete operator or create a new object without those properties. Let’s explore some… Read more: JavaScript – Remove specific property from an object
- JavaScript – Convert an object to an arrayThere are many ways to convert an object to an array in JavaScript. Let’s explore some common approaches. Convert Object Values to Array If you… Read more: JavaScript – Convert an object to an array