- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Removing duplicates and keep one instance in JavaScript
We are required to write a JavaScript function that takes in an array of literal values. The array might contain some repeating values inside it.
Our function should remove all the repeating values keeping the first instance of repeating value in the array.
Example
The code for this will be −
const arr = [1, 5, 7, 4, 1, 4, 4, 6, 4, 5, 8, 8]; const deleteDuplicate = (arr = []) => { for(let i = 0; i < arr.length; ){ const el = arr[i]; if(i !== arr.lastIndexOf(el)){ arr.splice(i, 1); } else{ i++; }; }; }; deleteDuplicate(arr); console.log(arr);
Output
And the output in the console will be −
[ 7, 1, 6, 4, 5, 8 ]
- Related Articles
- Removing duplicates and inserting empty strings in JavaScript
- Unique sort (removing duplicates and sorting an array) in JavaScript
- Removing adjacent duplicates from a string in JavaScript
- Removing duplicates from a sorted array of literals in JavaScript
- Removing consecutive duplicates from strings in an array using JavaScript
- Removing duplicates from tuple in Python
- Merge and remove duplicates in JavaScript Array
- Arranging lexicographically and removing whitespaces in JavaScript
- Remove duplicates and map an array in JavaScript
- Counting duplicates and aggregating array of objects in JavaScript
- Removing 0s from start and end - JavaScript
- Efficient algorithm for grouping elements and counting duplicates in JavaScript
- Removing last vowel - JavaScript
- How to delete duplicates and leave one row in a table in MySQL?
- Array.prototype.fill() with object passes reference and not new instance in JavaScript?

Advertisements