How to splice duplicate item in array JavaScript

We have an array of Number / String literals that contain some duplicate values, we have to remove these values from the array without creating a new array or storing the duplicate values anywhere else.

We will use the Array.prototype.splice() method to remove entries in-place, and we will take help of Array.prototype.indexOf() and Array.prototype.lastIndexOf() method to determine the duplicacy of any element.

The Problem with Forward Iteration

When using forEach() to remove duplicates, we encounter index shifting issues. Here's why the basic approach doesn't work perfectly:

const arr = [1, 4, 6, 1, 2, 5, 2, 1, 6, 8, 7, 5];
arr.forEach((el, ind, array) => {
    if(array.indexOf(el) !== array.lastIndexOf(el)){
        array.splice(ind, 1);
    }
});
console.log(arr);
[
    4, 1, 5, 2,
    6, 8, 7
]

As you can see, some duplicates remain because when we remove an element, subsequent elements shift left, causing the iteration to skip elements.

Solution: Reverse Iteration

To properly remove all duplicates, iterate backwards through the array. This prevents index shifting issues:

const arr = [1, 4, 6, 1, 2, 5, 2, 1, 6, 8, 7, 5];
console.log("Original:", arr);

for(let i = arr.length - 1; i >= 0; i--){
    if(arr.indexOf(arr[i]) !== arr.lastIndexOf(arr[i])){
        arr.splice(i, 1);
    }
}
console.log("After removing duplicates:", arr);
Original: [
  1, 4, 6, 1,  2,
  5, 2, 1, 6,  8,
  7, 5
]
After removing duplicates: [ 4, 8, 7 ]

How It Works

The logic uses two key methods:

  • indexOf() - Returns the first occurrence index of an element
  • lastIndexOf() - Returns the last occurrence index of an element

If these two values are different, the element appears multiple times and should be removed.

Alternative: Remove Only Extra Duplicates

If you want to keep one instance of each duplicate element:

const arr = [1, 4, 6, 1, 2, 5, 2, 1, 6, 8, 7, 5];
console.log("Original:", arr);

for(let i = arr.length - 1; i >= 0; i--){
    if(arr.indexOf(arr[i]) !== i){
        arr.splice(i, 1);
    }
}
console.log("Keeping first occurrence:", arr);
Original: [
  1, 4, 6, 1,  2,
  5, 2, 1, 6,  8,
  7, 5
]
Keeping first occurrence: [ 1, 4, 6, 2, 5, 8, 7 ]

Comparison

Method Result Keeps Duplicates?
Remove all duplicates Only unique elements remain No
Keep first occurrence First instance of each element One instance only

Conclusion

Use reverse iteration with splice() to safely remove duplicates in-place. Choose between removing all duplicates or keeping the first occurrence based on your requirements.

Updated on: 2026-03-15T23:18:59+05:30

711 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements