
- 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
Count and return the number of characters of str1 that makes appearances in str2 using JavaScript
Problem
We are required to write a JavaScript function that takes in two strings, str1 and str2 as the first and second argument respectively.
Our function should count and return the number of characters of str1 that makes appearances in str2 as well, and if there are repetitive appearances, we have to count them separately.
For example, if the input to the function is
Input
const str1 = 'Kk'; const str2 = 'klKKkKsl';
Output
const output = 5;
Example
Following is the code −
const str1 = 'Kk'; const str2 = 'klKKkKsl'; var countAppearances = (str1 = '', str2 = '') => { const map = {} for(let c of str1) { map[c] = true } let count = 0 for(let c of str2) { if(map[c]) { count+=1 } } return count }; console.log(countAppearances(str1, str2));
Output
5
- Related Articles
- Count appearances of a string in another - JavaScript
- Compare two arrays of single characters and return the difference? JavaScript
- How to count the number of characters (including spaces) in a text file using Java?
- Count the Number of matching characters in a pair of Java string
- Reverse the words in the string that have an odd number of characters in JavaScript
- Count the Number of matching characters in a pair of string in Python
- Finding count of special characters in a string in JavaScript
- Function that only replaces character from string after specified appearances in JavaScript
- C program to count characters, lines and number of words in a file
- How to count the number of repeated characters in a Golang String?
- Return the element that appears for second most number of times in the array JavaScript
- Program to count number of unique palindromes we can make using string characters in Python
- Count Number of Lowercase Characters in a String in Python Program
- Count number of factors of a number - JavaScript
- JavaScript Count characters case insensitive

Advertisements