-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
are-they-the-same.js
66 lines (56 loc) · 1.77 KB
/
are-they-the-same.js
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
function comp(array1, array2) {
if (!array1 ||
!array2 ||
array1.length != array2.length) return false;
// create an object where the keys are the squared values and the values are true
const squaredValues = array2.reduce((values, value) => {
values[value] = values[value] || 0;
values[value]++;
return values;
}, {});
// iterate over the first array
for (let i = 0; i < array1.length; i++) {
const value = array1[i];
const valueSqrd = value * value;
// if the current value squared is not in the object
if (!squaredValues[valueSqrd]) {
return false;
} else {
squaredValues[valueSqrd]--;
}
}
return true;
}
function comp(array1, array2) {
if (!array1 ||
!array2 ||
array1.length != array2.length) return false;
// create an object where the keys are the squared values and the values are true
const squaredValues = array2.reduce((values, value) => {
values[value] = values[value] || 0;
values[value]++;
return values;
}, {});
return array1.every(value => {
const valueSqrd = value * value;
if (!squaredValues[valueSqrd]) {
return false;
} else {
squaredValues[valueSqrd]--;
return true;
}
});
}
function comp(array1, array2) {
if (!array1 ||
!array2 ||
array1.length != array2.length) return false;
const sortNums = (a, b) => a - b;
const valuesSquaredAndSorted = array1.sort(sortNums).map(v => v * v);
const squaredSorted = array2.sort(sortNums);
// return valuesSquaredAndSorted.every((value, index) => value == squaredSorted[index]);
return valuesSquaredAndSorted.toString() == squaredSorted.toString();
}
const a1 = [88, 34, 66, 35, 7, 28, 83, 47];
const a2 = [7744, 1156, 4356, 1225, 49, 784, 6889, 2209];
console.log(comp(a1, a2), true);