 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP 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
Removing identical entries from an array keeping its length same - 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 four duplicate values, we have to remove then all and insert four empty strings at the end.
Example
Following is the code −
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
This will produce the following output in console −
[ 2, 3, 5, 12, 23, 4, 1, '', '', '', '', '', '', '', '', '', '', '' ]
Advertisements
 