JavaScript - Unique Values (remove duplicates) in an Array
Last Updated :
11 Jul, 2025
Given an array with elements, the task is to get all unique values from array in JavaScript. There are various approaches to remove duplicate elements, that are discussed below.
get all unique values (remove duplicates) in a JavaScript array1. Using set() - The Best Method
Convert the array into a Set in JS, which stores unique values, and convert it back to an array.
JavaScript
// Given array
let a = [ 10, 20, 20, 30, 40, 50, 50 ];
// Create set of unique values
// using Set constructor
let s = new Set(a);
// Convert back the set to array
let a1 = [...s]
// Display the updated array
console.log("Updated Array: ", a1);
OutputUpdated Array: [ 10, 20, 30, 40, 50 ]
2. Using for Loop with includes() Method
The for loop checked each value of the original array with each value of the output array where the duplicate values are removed. For checking the existing elements we will use the arry.includes() method.
JavaScript
// Given array
let a = [10, 20, 20, 20, 30, 30, 40, 50];
// Initialize epmty array to store unique values
let a1 = [];
// Iterate over the array to get unique values
for (let i = 0; i < a.length; i++) {
// Check if the element exist in the new array
if (!a1.includes(a[i])) {
// If not then push the element to new array
a1.push(a[i]);
}
}
// Display updated array
console.log("Updated Array: ", a1);
OutputUpdated Array: [ 10, 20, 30, 40, 50 ]
3. Using array.filter() with array.indexOf Method
The array.filter() method is used to create a new array from an existing array consisting of only those elements from the given array which satisfy a condition set by the argument function. Check the repeated element using the array.indexOf() method.
JavaScript
// Given array
let a = [10, 20, 20, 20, 30, 30, 40, 50];
// Use filter and return array with unique values
let a1 = a.filter((e, i, self) => i === self.indexOf(e));
// Display the updated array
console.log("Updated Array: ", a1);
OutputUpdated Array: [ 10, 20, 30, 40, 50 ]
Explore
JavaScript Basics
Array & String
Function & Object
OOP
Asynchronous JavaScript
Exception Handling
DOM
Advanced Topics