Created
July 10, 2020 03:32
-
-
Save erockdotdev/232baa0e24a61a2463c1c84676edd5f4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Given 2 positive integers find out if the 2 numbers have the same frequency of digits | |
function numberToArrStr(num){ | |
return num.toString().split(''); | |
}; | |
function incrementFrequency(obj){ | |
return function(n){ | |
obj[n] = ++obj[n] || 1 | |
}; | |
}; | |
function sameFrequency(num1, num2){ | |
const num1Frequency = {}; | |
const num2Frequency = {}; | |
const num1toArr = numberToArrStr(num1); | |
const num2toArr = numberToArrStr(num2); | |
if(num1toArr.length !== num2toArr.length) return false; | |
num1toArr.forEach(incrementFrequency(num1Frequency)); | |
num2toArr.forEach(incrementFrequency(num2Frequency)); | |
let isSame = true; | |
Object.keys(num1Frequency).forEach( n => { | |
if(num1Frequency[n] !== num2Frequency[n]) { | |
isSame = false | |
}; | |
}); | |
return isSame; | |
}; | |
sameFrquency(34, 44) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment