To remove duplicates in an Array in JavaScript, you can use the “Set”, “Array.prototype.filter()”, or Array.prototype.reduce() method.
Method 1: Using the Set
To remove duplicates from an array in JavaScript, convert an array into a Set using the “new Set()” constructor. The Set only contains unique values. So, after converting into Set, it will remove the duplicates and then convert them back into an array.
Example
let arr = ["Rohit", "Shubham", "Rahul", "Jeetu Bhaiya", "Shubham", "Rohit"]
function removeDuplicates(array) {
return [...new Set(array)];
}
console.log(removeDuplicates(arr));
Output
[ 'Rohit', 'Shubham', 'Rahul', 'Jeetu Bhaiya' ]
In this example, we created an array “arr” containing people’s names with some duplicates. Then we created a function “removeDuplicates()”, which takes an array and returns a unique array with no duplicate values. Then we print the returned array with the help of the console.log() function.
Method 2: Using the Array.prototype.filter() function
Using the Array.prototype.filter() function, we will create a function that will remove the duplicate values from the array and return a new array filled with unique values.
Example
let arr = ["Rohit", "Shubham", "Rahul", "Jeetu Bhaiya", "Shubham", "Rohit"]
function removeDuplicates(array) {
return array.filter((value, index) => array.indexOf(value) === index);
}
console.log(removeDuplicates(arr));
Output
[ 'Rohit', 'Shubham', 'Rahul', 'Jeetu Bhaiya' ]
In this example, we used a filter() method to filter the duplicates in the given array. First, we create a function, and inside that function, we create a condition inside the filter() method. After filtering an array, it will return the unique array we print using the console.log() function.
Method 3: Using the Array.prototype.reduce() function
The Array.prototype.reduce() method creates a new array with unique values by accumulating unique elements from the original array. It takes a callback function with an accumulator and the current value as parameters.
Example
const array = [1, 2, 3, 4, 4, 5, 6, 6, 7];
const uniqueArray = array.reduce((accumulator, value) => {
if (!accumulator.includes(value)) {
accumulator.push(value);
}
return accumulator;
}, []);
console.log(uniqueArray);
Output
[
1, 2, 3, 4,
5, 6, 7
]
All these methods will give you a new array with the duplicate values removed, keeping the original array unchanged.

Niva Shah is a Software Engineer with over eight years of experience. She has developed a strong foundation in computer science principles and a passion for problem-solving.