JavaScript Count repeating letter


In this problem statement, our target is to count the repeating letter in the given string with the help of Javascript functionalities. So we can solve this problem with the help of loops and some built-in methods of Javascript.

Logic for the given problem

In the given problem statement we have to design a program to count the repeating letters in the given string.

For implementing this task first we will create a blank object for count, array for repeated characters and another object for results. Then we will loop through every character in the string. So for every character we will check if it already exists in the count variable. If it is present then we will increment its value by 1 and then check if it has been added already to the repeated character array. If it is not there then we will add it to the array. And if the character does not exist in the count variable then we will add it with a count of 1.

So after the first loop, we will define another loop for repeated characters in the string and add every repeated character as a key to the result variable with the count value.

Algorithm

Step 1 − First we need to define a function to identify and count the repeated characters in a given string.

Step 2 − In the next step, we will create a blank object to store the count values in it. Second, create a blank array for storing the repeated characters array.

Step 3 − Then we will loop through every character in the input string with the help of built-in function charAt to access every character at every index and store it in a separate variable.

Step 4 − So after getting each character we need to check if it has been counted already. If it does exist, we increment its count by 1.

Step 5 − Additionally, we check if the character is repeated by checking if it already exists in the repeated array. If it is repeated then push it to the char object. otherwise keep its count value to 1.

Step 6 − Now create another object called result to get the repeated characters and its count values.

Step 7 − Again use a for loop to check the repeated characters and put those elements in the result object to get the required output.

Code for the algorithm

//function to find out the repeated letters
function repeatedLetters(str) {
   //count variable for repeated letters
   var count = {};
   var repeated = [];

   for (var i = 0; i < str.length; i++) {
      var char = str.charAt(i);
      if (count[char]) {
         count[char]++;
         if (repeated.indexOf(char) === -1) {
            repeated.push(char);
         }
      } else {
         count[char] = 1;
      }
   }
   var result = {};
   for (var i = 0; i < repeated.length; i++) {
      var char = repeated[i];
      result[char] = count[char];
   }
   return result;
}
console.log(repeatedLetters("Hello Tutorialspoint"));

Complexity

The time complexity for the implemented code is O(n). This complexity shows that the time taken to execute is proportional to the size of the input string. Because the code traverses the input string only once and performs basic operations on every character. Now the space complexity for the above code is O(n) in the worst case. Because the amount of memory used by the code grows linearly with the size of the input string. The code maintains a count for every character and it appears more than once in the input string which is increasing the memory usage.

Conclusion

As per the above code we have successfully implemented the problem statement with the help of Javascript. The code shows how to count the repeated characters in the given string. So we have used for loops to iterate through the characters. The code uses O(n) time to complete the execution.

Updated on: 18-May-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements