9. maps & sets

Que.1 You are given a string (STR) of length N, consisting of only the lower case English alphabet. Your task is to remove all the duplicate occurrences of characters in the string.

Input: abcadeecfb
Output: abcdef

  let Str = "abcadeecfb";
  const S = new Set();
  for (let i in Str) {
    S.add(Str[i]);
  }
  console.log(...S); // a b c d e f
        

Que.2 You are given a string (STR) of length N, you have to print the count of all alphabet.(using maps)

Input:abcadeecfb
Output:
a=2
b=2
c=2
d=1
e=2
f=1

            
  let Words = "abcadeecfb";
  let map = new Map();
  for (let i in Words) {
    if (map.has(Words[i])) {
      map.set(Words[i], Number(map.get(Words[i])) + 1);
    } else {
      map.set(Words[i], 1);
    }
  }
  for (const [key, value] of map) {
    console.log(key + " = " + value);
  }            
        
Back
Next