19 Feb 2023

A Javascript Program to find the count of unique string {a: 13, b: 12, c:13}

A Javascript Program to find the count of unique string -

Example:

var str = 'aaabbcccabcbcacbaabcbcabbccaaaabcbc';


Program: 

Part A:  Program to find the unique character in the given String -

var uniqLetter = [];

str.split('').map((item) =>{

    if(!uniqLetter.includes(item)){

        uniqLetter.push(item);

    }

});

Part B:  Now iterate on unique String & find the total count of the characetr in the given String & form the object -

var temp = {};

uniqLetter.map((i) => {

         temp = {...temp,

           ...{[i]: str.split(i).length}

           };

});

console.log(temp);

No comments:

Post a Comment