
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
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 inplace, and we will take help of Array.prototype.indexOf() and Array.prototype.lastIndexOf() method to determine the duplicacy of any element.
Example
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);
Output
The output in the console will be −
[ 4, 1, 5, 2, 6, 8, 7 ]
- Related Articles
- Find first duplicate item in array in linear time JavaScript
- Changing an array in place using splice() JavaScript
- How to splice an array without mutating the original Array?
- Remove elements from array in JavaScript using includes() and splice()?
- How to remove duplicate property values in array – JavaScript?
- How to find duplicate values in a JavaScript array?
- How to sort array by first item in subarray - JavaScript?
- How to remove elements using the splice() method in JavaScript?
- How to append an item into a JavaScript array?
- How to remove duplicate elements from an array in JavaScript?
- How to duplicate elements of an array in the same array with JavaScript?
- How to remove an item from JavaScript array by value?
- Get greatest repetitive item in array JavaScript
- How to redundantly remove duplicate elements within an array – JavaScript?
- Removing duplicate objects from array in JavaScript

Advertisements