Mahma Deva
Mahma Deva

Reputation: 566

How to splice duplicate item in array

I am making an attempt at writing a function in JavaScript that accepts an array that has duplicates in it and returns the same array removing duplicates. I know how to this using a Set, filter and reduce but I wanted to try to do it without using those functions. The problem is that I don't know how to splice the duplicate item once I have found them, so how can I just remove the item from the array if it is found as a duplicate, here is my code:

function clearDuplicatesInArray(arr, item) {
 for(i = 0; i < arr.length; i++) {
   for(j = 0; j < arr.length; j++) {
      if(arr[i] === arr[j]) {
       arr.splice(arr[i], arr[j])
      }
    }
  }
 return arr;
}

clearDuplicatesInArray([1,1,2,3]);

Upvotes: 5

Views: 2874

Answers (5)

treecon
treecon

Reputation: 2835

Another way of doing it by writing a single loop is the following:

1) Sort the array.

2) Run an reverse loop (to avoid problems with splicing).

arr = arr.sort();

let i = arr.length;
while (--i) {
    if (arr[i] == arr[i-1]) {
    arr.splice(i, 1);
  }
}

Upvotes: 0

LF-DevJourney
LF-DevJourney

Reputation: 28529

You can use the value as key to remove duplicate with O(n).

function clearDuplicatesInArray(array,item){
    var temp = {};
    for(v of array){
        temp[v] = v
    }
    return Object.values(temp)
}
console.log(clearDuplicatesInArray([1,1,1,2,1,4,2,3]))

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386620

You could iterate form the end, to prevent slicing for indices whixh are not visited yet and use a second loop until the outer index, because over this index, you have already visited the items.

For splicing, take the index and one as parameter, which removes one element at the given index.

function clearDuplicatesInArray(array, item) {
    var i = array.length,
        j;
    
    while (--i) {
        for (j = 0; j < i; j++) {
            if (array[i] === array[j]) {
                array.splice(i, 1);
                break;
            }
        }
    }
    return array;
}

console.log(clearDuplicatesInArray([1, 1, 2, 3]));

Upvotes: 5

Amit
Amit

Reputation: 1560

I am just going to fix your code, although you can get the solution in many different ways.

function clearDuplicatesInArray(arr, item) {
 for(i = 0; i < arr.length; i++) {
   for(j = i+1; j < arr.length; j++) {
      if(arr[i] === arr[j]) {
       arr.splice(i, 1);
       i--;
       break;
      }
    }
  }
 return arr;
}

console.log(clearDuplicatesInArray([1,1,2,3]));

Upvotes: 1

MiK
MiK

Reputation: 913

function removeDuplicates(array) {
   var tmp = array.slice(); //copy of array
   for(let i = 0, max = tmp.length; i < max; i++) {
      if(tmp.indexOf(tmp[i]) != tmp.lastIndexOf(tmp[i])) {
         tmp.splice(tmp.indexOf(tmp[i]), 1);
         i--;
      }
   }
   return tmp;
}

This function checks whether the first occurence of a value is also the last in the array, and if not, removes it.

Note that it uses slice and splice, 2 different functions.

input:
removeDuplicates([1,1,2,3,4,4,7]);

output:
Array(5) [ 1, 2, 3, 4, 7 ]

Upvotes: 0

Related Questions