-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy path04problem.js
More file actions
30 lines (26 loc) · 845 Bytes
/
04problem.js
File metadata and controls
30 lines (26 loc) · 845 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// Write a function called sameFrequency which takes two +ve integers
// and finds out if they have the same frequency of digits
// SAMPLE INPUT - (182,281) OUTPUT - true
function sameFrequency(num1, num2){
let strNum1 = num1.toString();
let strNum2 = num2.toString();
let frequencyNum1 = {};
if (strNum1.length !== strNum2.length) {
return false;
}
for (let i = 0; i < strNum1.length; i++) {
let number = strNum1[i];
frequencyNum1[number] ? frequencyNum1[number] += 1 : frequencyNum1[number] = 1;
}
for (let i = 0; i < strNum2.length; i++) {
let number = strNum2[i];
if (!frequencyNum1[number]) {
return false
}
else {
frequencyNum1[number] -= 1
}
}
return true;
}
console.log(sameFrequency(182,281));