-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsquaredValuesMatch.js
More file actions
30 lines (24 loc) · 760 Bytes
/
squaredValuesMatch.js
File metadata and controls
30 lines (24 loc) · 760 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
//takes in two arrays
function isThisSquared(original, squared) {
// add each number (squared) to object, increment count
// and then check for second array that the frequencies are the same
const frequencies = {};
if (original.length != squared.length) {
return false;
}
for (number of original) {
const square = number * number;
frequencies[square] = frequencies[square]++ || 1;
}
for (square of squared) {
if (frequencies[square]) {
frequencies[square] = frequencies[square] - 1;
} else {
return false;
}
}
return true;
}
console.log(isThisSquared([1, 2, 3], [4, 9, 1])); // true
console.log(isThisSquared([1, 2, 3], [9, 1])); // false
console.log(isThisSquared([1, 2, 1], [4, 4, 1])); // false