
- 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
Remove duplicate items from an array with a custom function in JavaScript
We are required to write a JavaScript function that takes in an array of literals. If an element appears for more than once in the array, the function remove all its instances from the array.
For example −
If the input array is −
const arr = [1,2,3,4,4,5,5];
Then the output should be −
const output = [1, 2, 3];
Example
const arr = [1, 2, 3, 4, 4, 5, 5]; const removeAll = (arr = [], val) => { while(arr.includes(val)){ const index = arr.indexOf(val); arr.splice(index, 1); }; }; const eradicateDuplicates = (arr = []) => { for(let i = 0; i < arr.length; ){ const el = arr[i]; if(arr.indexOf(el) === arr.lastIndexOf(el)){ i++; continue; }; removeAll(arr, el); }; }; eradicateDuplicates(arr); console.log(arr);
Output
And the output in the console will be −
[1, 2, 3]
- Related Articles
- Completely removing duplicate items from an array in JavaScript
- Remove duplicate items from an ArrayList in Java
- How to remove duplicate elements from an array in JavaScript?
- Remove the duplicate value from array with images data in JavaScript
- Remove/ filter duplicate records from array - JavaScript?
- Find the least duplicate items in an array JavaScript
- Using recursion to remove consecutive duplicate entries from an array in JavaScript
- Using recursion to remove consecutive duplicate entries from an array - JavaScript
- Swift Program to Remove Duplicate Elements From an Array
- Golang Program To Remove Duplicate Elements From An Array
- How to redundantly remove duplicate elements within an array – JavaScript?
- How do I recursively remove consecutive duplicate elements from an array?
- JavaScript function that should count all unique items in an array
- Remove values in an array by comparing the items 0th index in JavaScript?
- JavaScript Remove non-duplicate characters from string

Advertisements