
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Common Character Count in Strings in JavaScript
We are required to write a JavaScript function that takes in two strings, let us say str1 and str2.
The function should count the number of common characters that exist in the strings.
For example −
const str1 = 'aabbcc'; const str2 = 'adcaa';
Then the output should be 3
Example
Following is the code −
const str1 = 'aabbcc'; const str2 = 'adcaa'; const commonCharacterCount = (str1 = '', str2 = '') => { let count = 0; str1 = str1.split(''); str2 = str2.split(''); str1.forEach(e => { if (str2.includes(e)) { count++; str2.splice(str2.indexOf(e), 1); }; }); return count; }; console.log(commonCharacterCount(str1, str2));
Output
Following is the output on console −
3
- Related Articles
- Count common subsequence in two strings in C++
- Count common characters in two strings in C++
- Count the number of common divisors of the given strings in C++
- Count of sub-strings that contain character X at least once in C++
- Function to check two strings and return common words in JavaScript
- Finding the longest common consecutive substring between two strings in JavaScript
- Common Words in Two Strings in Python
- Greatest Common Divisor of Strings in Python
- Common words among tuple strings in Python
- Python – Sort by Rear Character in Strings List
- Count of strings that can be formed from another string using each character at-most once in C++
- How to count the number of occurrences of a character in a string in JavaScript?
- Formatted Strings Using Template Strings in JavaScript
- Template strings in JavaScript.
- Count occurrences of a character in string in Python

Advertisements