CommonCharacterCount | CodeFights Intro Algorithm JavaScript Solution and Breakdown

preview_player
Показать описание
CommonCharacterCount | CodeFights Intro Algorithm JavaScript Solution and Breakdown
Support our Community:

Tutoring, Mentoring and Course Walkthroughs available:

Thank you to my Patreon Supporters Below:

Support me and visit my store at:

Internship, Part-Time, & Full time work for college students and recent grads:

Fan funding goes towards buying the equipment necessary to deliver 4k videos, 4k webcam, and a high quality microphone better audio. Any support is very appreciated!

My channel is here for aspiring programmers to learn easier and help debug any issues from the many great free resources available on the web.

Check out my other videos going over HTML, CSS, Ruby, Ruby on Rails, Java, JavaScript, Python, PHP, SQL, Command Line, BootStrap, jQuery, and WordPress from CodeCademy, CodeCombat, FreeCodecamp and more!

-~-~~-~~~-~~-~-
Please watch: "How I Became a Developer | My Developer Journey of the Last 3 Years | Ask a Dev"
-~-~~-~~~-~~-~-
Рекомендации по теме
Комментарии
Автор

i used map and then converted to object and then did what u did from there

ilovepizza
Автор

That's so awesome. I spend much time to handle the issue but can't solve, now I got it. Thank you so much, keep going Bro

positive-yd
Автор

below is cleaner way,  
const commonCharactersCount = (s1, s2) => {
let string1 = s1.split('');
let string2 = s2.split('');
let arrayOftMatch = [];
string1.forEach(element1 => {
string2.forEach(element2 => {
if ((element1 === element2) && == -1)) {
arrayOftMatch.push(element2);
}
})
});
return arrayOftMatch.length;
}

husamalsairi
Автор

Just more refactored code:
function commonCharacterCount(s1, s2) {
const s1Count={};
const s2Count={};
let count = 0;
for(let char of s1){
s1Count[char] = s1Count[char] + 1 || 1;
}
for(let char of s2){
s2Count[char] = s2Count[char] + 1 || 1;
}
for(let char in s1Count){
){
count += Math.min(s1Count[char], s2Count[char])
}
}
return count;

}

lakhanrochwani
Автор

Using objects makes the function kind of long. Here is what I did:

function commonCharacterCount(s1, s2){
var s2Arr = s2.split('');
var result = 0;

for(var i=0; i<s1.length; i++){
if(s2Arr.indexOf(s1[i]) !== -1){
s2Arr.splice(s2Arr.indexOf(s1[i]), 1);
result++;
}
}

return result;
}

young
Автор

Can this be solved with a map function?

HowYoongJian
join shbcf.ru