how to remove duplicate values from a given array

Remove Duplicate Elements From An Array In JavaScript

Summary: Hey guys, in this blog we are going to learn how to Remove Duplicate Elements From An Array In JavaScript. There are many ways of removing the re-occurring values inside an array. We can create a function for that or we simply can use the built-in array methods for this. So here in this tutorial, we are going to look through some of the methods to delete a duplicate item from the array in javascript.

Remove Duplicate Elements From An Array

01) The first method we are using is the indexOf() array method with a function

  • Here the indexOf() method returns the index of the first occurrence of an element in an array.
  • So to start, we create a function and take the array as its argument.
  • Then we create a separate variable to hold our unique values.
  • Finally, we will loop/iterate through the array and then will check the index for the first occurrence of the element with the indexOf() method.
  • And we will push the value to the unique value holder variable.
let arrayWithDuplicateValues = ['A','B','C','B','D','A','E','C','A','B','D','F','A'];

const deleteDuplicateItemFromArray = (arr) =>{
	const unqiueArr = [];
	
	for(let i =0; i < arr.length; i++){
    	if(uniqureArr.indexOf(arr[i]) !== -1){
        	uniqueArr.push(arr[i])
        }
    }

	return uniqueArr;

}

let newUniqueValues = deleteDuplicateItemFromArray(arrayWithDuplicateValues);

console.log(newUniqueValues);

Output

// output ==> ['A','B','C','D','E','F']

02 ) Remove duplicate values from an array with the Set() function

  • We at first will convert the array of duplicates to a Set.
  • The new Set will implicitly return the unique values from the array.
let arrayWithDuplicateValues = ['A','B','C','B','D','A','E','C','A','B','D','F','A'];

const newArr = [... new Set(arrayWithDuplicateValues)];

console.log(newArr);

03 ) Removing the duplicate values from an array with filter() and indexOf() method

  • First, we check the duplicate value in the array.
  • So we use the indexof() method as it returns the index of the first occurrence of the item.
  • To eliminate any duplicate values inside our array, we use the filter() function to include only elements whose indices match their indexOf values.
let arrayWithDuplicateValues = ['A','B','C','B','D','A','E','C','A','B','D','F','A'];

const filteredItem = arrayWithDuplicateValues.filter((array, index)=>{
	return arrayWithDuplicateValues.indexOf(array) === index
})

const getDuplicateValue = arrayWithDuplicateValues.filter((array, index)=>{
	return arrayWithDuplicateValues.indexOf(array) !== index
})

console.log(filteredItem); // output ==> ['A','B', 'C', 'D', 'E', 'F']
console.log(getDuplicateValue); // output ==> ['A','B','C','D']

04 ) Removing the duplicate values from an array using includes() method

  • The includes() method checks if an array has a certain value among its items and returns true or false as appropriate.
  • We use the forEach method to loop through each of the values inside an array.
  • The example below iterates through the values inside the array and adds the values if it is not already present in the array.
let arrayWithDuplicateValues = ['A','B','C','B','D','A','E','C','A','B','D','F','A'];

let uniqueValues = [];
arrayWithDuplicateValues.forEach((element) => {
	if(!uniqueValues.includes(element)){
    	uniqueValues.push(element)
    }
})
console.log(uniqueValues)

So these are some of the ways to: Remove Duplicate Elements From An Array In JavaScript

So these are some ways to remove the duplicate values from the array. This sort of array of related questions is regularly asked in the interview. So, you can simply read this blog and prepare for the interviews.

So that’s it from this blog on array manipulations. Hope you guy’s like this blog.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.