Common Character Count in Strings in JavaScript

In JavaScript, finding common characters between two strings involves counting how many characters appear in both strings. This is useful for text analysis, similarity comparisons, and string manipulation tasks.

Understanding the Problem

The goal is to count common characters between two strings, considering frequency. For example, if we have "abaac" and "baaaa", the common characters are 'a' (appears 3 times in first string, 4 times in second, so we count 3) and 'b' (appears 1 time in both, so we count 1), giving us a total of 4 common characters.

Algorithm

Step 1: Create a function that accepts two string parameters.

Step 2: Initialize a counter variable to track common characters.

Step 3: Convert strings to arrays for easier manipulation.

Step 4: Iterate through the first string's characters.

Step 5: For each character, check if it exists in the second string.

Step 6: If found, increment the counter and remove that character from the second array to avoid double counting.

Step 7: Return the final count.

Example

// Function to count common characters between two strings
function commonCharCount(str1, str2) {
   let count = 0;
   const arr1 = str1.split('');
   const arr2 = str2.split('');

   for (let i = 0; i < arr1.length; i++) {
      const char = arr1[i];
      const index = arr2.indexOf(char);

      if (index !== -1) {
         arr2.splice(index, 1);
         count++;
      }
   }

   return count;
}

// Test with different examples
const str1 = "abaac";
const str2 = "baaaa";
console.log("Common characters in '" + str1 + "' and '" + str2 + "':", commonCharCount(str1, str2));

const str3 = "hello";
const str4 = "world";
console.log("Common characters in '" + str3 + "' and '" + str4 + "':", commonCharCount(str3, str4));
Common characters in 'abaac' and 'baaaa': 4
Common characters in 'hello' and 'world': 3

Alternative Approach Using Map

For better performance with larger strings, we can use a Map to count character frequencies:

function commonCharCountOptimized(str1, str2) {
   const charMap1 = new Map();
   const charMap2 = new Map();
   
   // Count characters in first string
   for (let char of str1) {
      charMap1.set(char, (charMap1.get(char) || 0) + 1);
   }
   
   // Count characters in second string
   for (let char of str2) {
      charMap2.set(char, (charMap2.get(char) || 0) + 1);
   }
   
   let commonCount = 0;
   
   // Find common characters and their minimum frequency
   for (let [char, count1] of charMap1) {
      if (charMap2.has(char)) {
         const count2 = charMap2.get(char);
         commonCount += Math.min(count1, count2);
      }
   }
   
   return commonCount;
}

console.log("Optimized result:", commonCharCountOptimized("abaac", "baaaa"));
Optimized result: 4

Comparison

Method Time Complexity Space Complexity Best For
Array Splicing O(n²) O(n) Small strings
Map-based O(n) O(n) Large strings

Conclusion

Counting common characters between strings can be achieved using array manipulation or Map-based frequency counting. The Map approach offers better performance for larger strings with O(n) time complexity.

Updated on: 2026-03-15T23:19:00+05:30

745 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements