JavaScript - Unique Values (remove duplicates) in an Array Last Updated : 14 Nov, 2024 Comments Improve Suggest changes Like Article Like Report 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 MethodConvert 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() MethodThe 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 MethodThe 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 ] Comment More infoAdvertise with us Next Article JavaScript - Unique Values (remove duplicates) in an Array S Stranger1 Follow Improve Article Tags : JavaScript Web Technologies javascript-array JavaScript-DSA JavaScript-Set +1 More Similar Reads How to Remove Duplicates in JSON Array JavaScript ? In JavaScript, removing duplicates from a JSON array is important for data consistency and efficient processing. We will explore three different approaches to remove duplicates in JSON array JavaScript. Use the methods below to remove Duplicates in JSON Array JavaScript. Table of Content Using SetUs 3 min read How to Remove Duplicate Objects from an Array in JavaScript? In JavaScript, it's a common example that the arrays contain objects and there might be a possibility that the objects may or may not be unique. Removing these duplicate objects from the array and getting the unique ones is a common task in Web Development. These are the following approaches: Table 2 min read JavaScript Get all non-unique values from an array We have given a JavaScript array, and the task is to find all non-unique elements of the array. To get all non-unique values from the array here are a few examples. These are the following methods to get all non-unique values from an array:Table of ContentUsing Array Slice() MethodUsing for loopUsin 4 min read How to filter out the non-unique values in an array using JavaScript ? In JavaScript, arrays are the object using the index as the key of values. In this article, let us see how we can filter out all the non-unique values and in return get all the unique and non-repeating elements. These are the following ways by which we can filter out the non-unique values in an arra 4 min read How to Remove Duplicates from an Array of Objects in JavaScript? Here are some effective methods to remove duplicates from an array of objects in JavaScript1. Using filter() and findIndex() Methods - Most UsedThe simplest way to remove duplicates is by using filter() and findIndex(). This method keeps the first occurrence of each object with a unique property (li 3 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 duplicate elements from array in JavaScript ? In this article, we will discuss the methods to remove duplicate elements from a Javascript array. There are various methods to remove duplicates in the array. These are the following ways: Table of Content Using filter() MethodUsing set() MethodUsing forEach() MethodUsing reduce() MethodUsing index 4 min read How to remove duplicate values from array using PHP? In this article, we will discuss removing duplicate elements from an array in PHP. We can get the unique elements by using array_unique() function. This function will remove the duplicate values from the array.Syntax:array array_unique($array, $sort_flags);Note: The keys of the array are preserved i 4 min read How to Merge Two Arrays and Remove Values that have Duplicates? In this article, we will discuss how to merge two arrays and remove values that have duplicate Ruby. We can merge two arrays and remove values that have duplicates through various methods provided in Ruby. Table of Content Using the | OperatorUsing the uniq MethodUsing the Concat with uniq MethodUsi 2 min read JavaScript - How To Get Distinct Values From an Array of Objects? Here are the different ways to get distinct values from an array of objects in JavaScript1. Using map() and filter() MethodsThis approach is simple and effective for filtering distinct values from an array of objects. You can use map() to extract the property you want to check for uniqueness, and th 4 min read Like