- 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 every one of the two strings that start with the same letter in JavaScript
We are required to write a JavaScript function that takes in an array of strings and deletes every one of the two strings that start with the same letter.
For example, If the actual array is −
const arr = ['Apple', 'Jack' , 'Army', 'Car', 'Jason'];
Then we have to delete and keep only one string in the array distinct letters, so one of the two strings starting with A should get deleted and the same should the one with J.
Example
The code for this will be −
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
The output in the console −
[ 'Apple', 'Jack', 'Car' ]
- Related Articles
- Change every letter to next letter - JavaScript
- Count of strings that become equal to one of the two strings after one removal in C++
- Missing letter from strings in JavaScript
- Finding letter distance in strings - JavaScript
- Counting substrings of a string that contains only one distinct letter in JavaScript
- Delete Operations for Two Strings in C++
- Delete duplicate elements based on first letter – JavaScript
- How to put space between words that start with uppercase letter in string vector in R?
- How to match two strings that are present in one line with grep in Linux?
- Meta Strings (Check if two strings can become same after a swap in one string) in C++
- How to compare two strings in the current locale with JavaScript?
- Program to append two given strings such that, if the concatenation creates a double character then omit one of the characters - JavaScript
- Find the dissimilarities in two strings - JavaScript
- Different substrings in a string that start and end with given strings in C++ Program
- Minimum ASCII Delete Sum for Two Strings in C++

Advertisements