Remove Duplicate Elements from JavaScript Array Last Updated : 09 Jan, 2025 Comments Improve Suggest changes Like Article Like Report To Remove the elements from an array we can use the JavaScript set method. Removing duplicate elements requires checking if the element is present more than one time in the array. 1. Using JavaScript Set() - Mostly UsedThe JavaScript Set() method creates an object containing only unique values. To remove the duplicate elements we can use the set and then convert the object back to array to get the only unique values. JavaScript let a = [ 10, 20, 20, 30, 40, 50, 50 ]; // Create set of unique values using Set constructor let s = new Set(a); let a1 = [...s] console.log(a1); Output[ 10, 20, 30, 40, 50 ] The duplicate elements are removed as shown below:2. Using JavaScript filter() Method with indexOf methodThe array.filter() method creates a new array of elements that matches the passed condition through the callback function. It will include only those elements for which true is returned.To check the repeated elements we will use the array.idexOf method. If it returns same index then push the elements else skip the element and return the updated array. JavaScript let a = [10, 20, 20, 30, 40, 50, 50]; // Using filter with indexOf to find the repeated elements a1 = a.filter((item, index) => a.indexOf(item) === index); console.log(a1); Output[ 10, 20, 30, 40, 50 ] Mastering such array operations is key to writing efficient JavaScript. 3. Using JavaScript forEach() with array.includes() methodBy using the forEach() method, we can iterate over the elements in the array, and we will push into the new array if it doesn’t exist in the array. To check the element in the new array we will use the array.includes() method, it returns -1 if the element is not present. JavaScript let a = [10, 20, 20, 30, 40, 50, 50]; let a1 = []; a.forEach(element => { if (!a1.includes(element)) { a1.push(element); } }); console.log(a1); Output[ 10, 20, 30, 40, 50 ] 4. Using JavaScript reduce() with array.includes() methodThe reduce() method is used to reduce the elements of the array and combine them into a final array based on some reducer function that you pass. To check the element in the accumulator array we will use the array.includes() method, it returns -1 if the element is not present. JavaScript let a = [10, 20, 20, 30, 40, 50, 50]; let a1 = a.reduce(function (acc, curr) { if (!acc.includes(curr)) { acc.push(curr); } return acc; }, []); console.log(a1) Output[ 10, 20, 30, 40, 50 ] Using third-party LibraryWe can also use a third-party library such as Lodash or Underscore.js to remove duplicate elements from a Javascript array. The _.uniq() function returns the array which does not contain duplicate elements. HTML <!DOCTYPE html> <html> <head> <script src= "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"> </script> </head> <body> <script type="text/javascript"> console.log(_.uniq([1, 2, 3, 4, 5, 4, 3, 2, 1])); </script> </body> </html> Output: Comment More infoAdvertise with us Next Article How to Remove Multiple Elements from Array in JavaScript? rohit_iiitl Follow Improve Article Tags : JavaScript Web Technologies TrueGeek TrueGeek-2021 javascript-array JavaScript-DSA JavaScript-Questions +3 More Similar Reads How to Add Elements to a JavaScript Array? Here are different ways to add elements to an array in JavaScript.1. Using push() MethodThe push() method adds one or more elements to the end of an array and returns the new length of the array.Syntaxarray.push( element1, element2, . . ., elementN );JavaScriptconst arr = [10, 20, 30, 40]; arr.push( 3 min read Create an Array of Given Size in JavaScript The basic method to create an array is by using the Array constructor. We can initialize an array of certain length just by passing a single integer argument to the JavaScript array constructor. This will create an array of the given size with undefined values.Syntaxconst arr = new Array( length );J 3 min read Insert at the Beginning of an Array in JavaScript Following are different ways to add new elements at the beginning of an array1. Using the Array unshift() Method - Most Used:Adding new elements at the beginning of the existing array can be done by using the Array unshift() method. This method is similar to the push() method but it adds an element 2 min read Remove Elements From a JavaScript Array Here are the various methods to remove elements from a JavaScript ArrayRemove elements from Array1. Using pop() methodThe pop() method removes and returns the last element of an array. This function decreases the length of the array by 1 every time the element is removed.javascriptlet a = ["Apple", 6 min read Remove Duplicate Elements from JavaScript Array To Remove the elements from an array we can use the JavaScript set method. Removing duplicate elements requires checking if the element is present more than one time in the array. 1. Using JavaScript Set() - Mostly UsedThe JavaScript Set() method creates an object containing only unique values. To r 3 min read How to Remove Multiple Elements from Array in JavaScript? Here are various methods to remove multiple elements from an array in JavaScript1. Using filter() MethodThe filter() method creates a new array with the elements that pass the condition. This method does not change the original array.JavaScriptlet a = [1, 2, 3, 4, 5]; let remove = [2, 4]; a = a.filt 3 min read Insert at the Beginning of an Array in JavaScript Following are different ways to add new elements at the beginning of an array1. Using the Array unshift() Method - Most Used:Adding new elements at the beginning of the existing array can be done by using the Array unshift() method. This method is similar to the push() method but it adds an element 2 min read Reverse an Array in JavaScript Here are the different methods to reverse an array in JavaScript1. Using the reverse() MethodJavaScript provides a built-in array method called reverse() that reverses the elements of the array in place. This method mutates the original array and returns the reversed array.JavaScriptlet a = [1, 2, 3 3 min read JavaScript - Delete last Occurrence from JS Array These are the following ways to remove the last Item from the given array: 1. Using pop() Method (Simple and Easiest Method for Any Array) The pop() method is used to get the last element of the given array it can also be used to remove the last element from the given array. JavaScriptlet a = [34, 2 2 min read Remove Empty Elements from an Array in JavaScript Here are different approaches to remove empty elements from an Array in JavaScript.1. Using array.filter() MethodThe array filter() method is used to create a new array from a given array consisting of elements that satisfy given conditions.array.filter( callback( element, index, arr ), thisValue )J 3 min read Like