Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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"));
{ l: 2, o: 3, t: 4, i: 2, a: 2, r: 2, s: 2 }
Alternative Approach Using Modern JavaScript
We can simplify the solution using modern JavaScript features like for...of loop and Object.entries:
function countRepeatedLetters(str) {
const count = {};
// Count all characters
for (const char of str) {
count[char] = (count[char] || 0) + 1;
}
// Filter only repeated characters
const repeated = Object.fromEntries(
Object.entries(count).filter(([char, freq]) => freq > 1)
);
return repeated;
}
console.log(countRepeatedLetters("Hello Tutorialspoint"));
console.log(countRepeatedLetters("JavaScript"));
{ l: 2, o: 3, t: 4, i: 2, a: 2, r: 2, s: 2 }
{ a: 4, S: 2, c: 2, r: 2, p: 2 }
Case-Insensitive Version
If you want to count letters regardless of their case:
function countRepeatedLettersIgnoreCase(str) {
const count = {};
const lowerStr = str.toLowerCase();
for (const char of lowerStr) {
count[char] = (count[char] || 0) + 1;
}
return Object.fromEntries(
Object.entries(count).filter(([char, freq]) => freq > 1)
);
}
console.log(countRepeatedLettersIgnoreCase("JavaScript"));
{ j: 2, a: 4, v: 2, s: 2, c: 2, r: 2, i: 2, p: 2, t: 2 }
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
We have successfully implemented solutions to count repeating letters in a string using JavaScript. The traditional approach uses loops and objects, while the modern approach leverages features like Object.entries() and filter() for cleaner code. Both solutions have O(n) time complexity and work efficiently for finding duplicate characters.
