- 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
Delete duplicate elements based on first letter – JavaScript
We are required to write a JavaScript function that takes in array of strings and delete every one of the two string that start with the same letter.
For example, If the actual array is −
const arr = ['Apple', 'Jack' , 'Army', 'Car', 'Jason'];
Then, we have to keep only one string in the array, so one of the two strings starting with A should get deleted. In the same way, the logic follows for the letter J in the above array.
Let’s write the code for this function −
Example
const arr = ['Apple', 'Jack' , 'Army', 'Car', 'Jason']; const delelteSameLetterWord = arr => { const map = new Map(); arr.forEach((el, ind) => { if(map.has(el[0])){ arr.splice(ind, 1); }else{ map.set(el[0], true); }; }); }; delelteSameLetterWord(arr); console.log(arr);
Output
Following is the output in the console −
[ 'Apple', 'Jack', 'Car' ]
- Related Articles
- Grouping names based on first letter in JavaScript
- Deleting the duplicate strings based on the ending characters - JavaScript
- Splitting an array based on its first value - JavaScript
- Capitalizing first letter of each word JavaScript
- C Program to delete the duplicate elements in an array
- Delete elements in first string which are not in second string in JavaScript
- Sorting array based on increasing frequency of elements in JavaScript
- Array filtering using first string letter in JavaScript
- Make first letter of a string uppercase in JavaScript?
- Program to find duplicate elements and delete last occurrence of them in Python
- Constructing an array of smaller elements than the corresponding elements based on input array in JavaScript
- Styling First-Letters with CSS ::first-letter
- Select all duplicate MySQL rows based on one or two columns?
- Capitalize only the first letter after colon with regex and JavaScript?
- How to delete an array element based on key in PHP?

Advertisements