
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Remove duplicates from an array keeping its length same in JavaScript
We have to write a function that takes in an array, removes all duplicates from it and inserts the same number of empty strings at the end.
For example − If we find 4 duplicate values we have to remove then all and insert four empty strings at the end.
Therefore, let’s write the code for this problem −
Example
const arr = [1,2,3,1,2,3,2,2,3,4,5,5,12,1,23,4,1]; const deleteAndInsert = arr => { const creds = arr.reduce((acc, val, ind, array) => { let { count, res } = acc; if(array.lastIndexOf(val) === ind){ res.push(val); }else{ count++; }; return {res, count}; }, { count: 0, res: [] }); const { res, count } = creds; return res.concat(" ".repeat(count).split(" ")); }; console.log(deleteAndInsert(arr));
Output
The output in the console will be −
[ 2, 3, 5, 12, 23, 4, 1, '', '', '', '', '', '', '', '', '', '', '' ]
- Related Questions & Answers
- Removing identical entries from an array keeping its length same - JavaScript
- Add two array keeping duplicates only once - JavaScript
- Remove duplicates and map an array in JavaScript
- Remove duplicates from a array of objects JavaScript
- Remove duplicates from array with URL values in JavaScript
- Java Program to Remove Duplicates from an Array List
- The best way to remove duplicates from an array of objects in JavaScript?
- Remove array duplicates by property - JavaScript
- Remove Duplicates from Sorted Array in Python
- Merge and remove duplicates in JavaScript Array
- Remove Duplicates from Sorted Array II in C++
- Maximum array from two given arrays keeping order same in C++
- How to remove duplicates from the sorted array and return the length using C#?
- Remove same values from array containing multiple values JavaScript
- Remove axes of length one from an array in Numpy
Advertisements