-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsameFrequency.js
59 lines (55 loc) · 1.52 KB
/
sameFrequency.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
//Frequency counter - Problem solving pattern
// My solution
// function same(array1, array2){
// if(array1.length != array2.length){
// return false;
// } else {
// for(let itemInArr1 of array1){
// if(!array2.includes(itemInArr1 ** 2)){
// return false;
// }
// }
// return true;
// }
// }
//Naive solution with Time Complexity - O(N^2)
// function same(arr1, arr2){
// if(arr1.length !== arr2.length){
// return false;
// }
// for(let i = 0; i < arr1.length; i++){
// let correctIndex = arr2.indexOf(arr1[i] ** 2)
// if(correctIndex === -1) {
// return false;
// }
// arr2.splice(correctIndex,1)
// }
// return true
// }
//Time complexity - O(N)
function same(arr1, arr2){
if(arr1.length !== arr2.length){
return false;
}
let frequencyCounter1 = {}
let frequencyCounter2 = {}
for(let val of arr1){
frequencyCounter1[val] = (frequencyCounter1[val] || 0) + 1
}
for(let val of arr2){
frequencyCounter2[val] = (frequencyCounter2[val] || 0) + 1
}
for(let key in frequencyCounter1){
if(!(key ** 2 in frequencyCounter2)){
return false
}
if(frequencyCounter2[key**2] !== frequencyCounter1[key]){
return false
}
}
return true;
}
let time1 = performance.now();
same([1,2,3],[1,4,9]);
let time2 = performance.now();
console.log(`Time Elapsed: ${(time2 - time1) / 1000} seconds.`)