JSFiddle - React, Tailwind, and code Playground
JavaScript
var firstString = "hello world 32";
var secondString = "FOLLOW me 231";
var firstChars = [];
var secondChars = [];
var arrLength = 122;
// 0 - 122 covers ASCII values for all chars a-z (lowers), A-Z (uppers), numbers, and empty spaces.
//initialize array to arrLength number of elements, all set to 0.
for (var i = 0; i < arrLength; i++)
{
firstChars.push(0);
secondChars.push(0);
}
//count first word chars
for (var i = 0; i < firstString.length; i++)
firstChars[firstString.charCodeAt(i)]++;
//count second word chars
for (var i = 0; i < secondString.length; i++)
secondChars[secondString.charCodeAt(i)]++;
//output matching chars and the count of occurences.
for (var i = 0; i < arrLength; i++)
{
if (firstChars[i] > 0 && secondChars[i] > 0)
{
var letter = i == 32? "Empty Space" : String.fromCharCode(i);
var count = firstChars[i] + secondChars[i];
console.log(letter + " : " + count);
}
}