Skip to content

Instantly share code, notes, and snippets.

@bflannery
Created February 3, 2019 13:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bflannery/acd07fd87c41bede67de13f77c61c237 to your computer and use it in GitHub Desktop.
Save bflannery/acd07fd87c41bede67de13f77c61c237 to your computer and use it in GitHub Desktop.
Frequency Counter Challenge - sameFrequency
// Frequency Counter - sameFrequency
// Write a function called sameFrequency. Given two positive intergers,
// find out if the two numbers have the same frequency of digits.
// Solution Requirements
// - Time Complexity: O(n)
function sameFrequency(int1, int2) {
const int1Arr = Array.from(int1.toString()).map(Number);
const int2Arr = Array.from(int2.toString()).map(Number);
if(int1Arr.length !== int2Arr.length) return false
let refObject = {}
for(var int1Val of int1Arr) {
refObject[int1Val] = refObject[int1Val] ? refObject[int1Val] += 1 : refObject[int1Val] = 1
}
for(var int2Val of int2Arr) {
if (!refObject[int2Val]) return false
refObject[int2Val] =- 1
}
return true;
}
// console.log(sameFrequency(182, 281)) // true
// console.log(sameFrequency(34, 14)) // false
// console.log(sameFrequency(3589578, 5879385)) // true
// console.log(sameFrequency(22, 222)) // true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment