Top 5 ways to remove duplicates from an Array

Top 5 ways to remove duplicates from an Array

Array is the most useful data structure to store the data and its widely used across platforms. Whenever you're appearing for an interview for javascript, this could be the first question to check your logical ability.

Here are the top five ways to remove the duplicates without hassle.

  1. Using the brute force approach, the simplest one and beginner-friendly. You just need to iterate two loops and check the adjacent elements for equality.

     var array = [10, 20, 20, 30, 10, 30, 40, 58, 31, 32, 31]
    
     var unique = [];
    
     for (let i = 0; i < array.length; i++) {
        let isDuplicate = false;
        for (let j = i+1; j < array.length; j++) {
           if(array[i]==array[j]){
              isDup=true
              break;
           }
        }
        if(!isDuplicate){
           unique.push(array[i])         
        }
     }
    
     console.log(unique)
    
  2. It can also be done using the filter method where the comparison between the first index of the element and the current index of the element has to be the same.

    Here, the indexOf() method returns the first index of the element.

     var array = [ 10, 30, 10, 20, 30, 40, 10]
    
     for(let i =0; i<array.length; i++){
         console.log(array.indexOf(i))
     }
    
     Num  |IndexOf| Iterator
     10   | 0     | 0
     30   | 1     | 1
     10   | 0     | 2
     20   | 3     | 3
     30   | 1     | 4
     40   | 5     | 5
     10   | 0     | 6
    

    So, we just have to compare the first index should be the same index of that element whenever it occurs in the array.

     function removeDuplicates2(array){
        let unique = array.filter((el, index)=>{
           return(array.indexOf(el) === index)
        })
        console.log("Unique Arrays", unique)
     }
    
     removeDuplicates2([0, 0, 1, 1, 1, 2, 2, 3, 3, 4]);
    
  3. Also, there's a shortest method from ES6 that can give you an array with unique values.

     function removeDuplicates3(array){
        let unique = [..new Set(array)]
        console.log("Unique Arrays", unique)
     }
    
     removeDuplicates3([0, 0, 1, 1, 1, 2, 2, 3, 3, 4]);
    
  4. You can create a separate array and check if the element exists in that array or not.

     function removeDuplicates4(array){
        let unique = []
        array.forEach((el, index)=>{
         if(!unique.includes(el)){
             unique.push(el)
         }
        })
        console.log("Unique Arrays", unique)
     }
    
     removeDuplicates4([0, 0, 1, 1, 1, 2, 2, 3, 3, 4]);
    
  5. The last but not the least is the reduce method from ES6 again. This method has a callback function with two parameters such as accumulater and current element.

    Also, we have to define the initial value of the accumulater as the blank array. The condition would be to check the element exists in the accumulator or not, and if doesn't exist then it will return -1.

     function removeDuplicates5(array){
        let unique = array.reduce((acc, current)=>{
           if(acc.indexOf(current) === -1){
              acc.push(current)
           }
           return acc
        },[])
        console.log("Unique Arrays", unique)
     }
    
     removeDuplicates5([0, 0, 1, 1, 1, 2, 2, 3, 3, 4]);
    

    So, that's it for now, hope you found this article useful for your learning journey, If its beneficial for you then dont forget to click the upvote button. It will motivate me to write more. Cheers!

Did you find this article valuable?

Support Ajinkya Chanshetty by becoming a sponsor. Any amount is appreciated!