-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsame.js
More file actions
30 lines (24 loc) · 709 Bytes
/
Copy pathsame.js
File metadata and controls
30 lines (24 loc) · 709 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 which accepts two arrays. The function
// should return true if every value in the array has
// it's corresponding value squared in the second array.
let same = (arr1, arr2) => {
let storage = {}
let counter = 0
for (let i = 0; i < arr1.length; i++) {
let key = Math.pow(arr1[i], 2)
storage[key] = key
counter++
}
for (let i = 0; i < arr2.length; i++) {
if (storage[arr2[i]]) {
counter--
} else {
return false
}
}
return counter === 0
}
console.log(same([1, 2, 3], [1, 4, 9, 11]))
console.log(same([1, 2, 3], [1, 9]))
console.log(same([2, 5, 3], [25, 4, 9]))
// This solution is O(n) as opposed to double looping which would be O(n2)