-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathisSame.js
More file actions
34 lines (31 loc) · 868 Bytes
/
isSame.js
File metadata and controls
34 lines (31 loc) · 868 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
31
32
33
34
const isSame = (arr1, arr2) => {
if(arr1.length !== arr2.length) return false;
for(let i = 0; i < arr1.length; i++) {
let index = arr2.indexOf(arr1[i])
if (index === -1 ) {
return false;
} else {
arr2.slice(index, 1)
}
}
return true;
}
console.log(isSame([1, 2, 4, 5], [1, 4, 6, 2]))
const isSameOptimized = (arr1, arr2) => {
if(arr1.length !== arr2.length) return false;
const hashTable1 = {}
const hashTable2 = {}
for (let num of arr1) {
hashTable1[num] = (hashTable1[num] || 0) + 1
}
for (let num of arr2) {
hashTable2[num] = (hashTable2[num] || 0) + 1
}
for (let elm in hashTable1){
if( !elm in hashTable2 || hashTable1[elm] !== hashTable2[elm]) {
return false
}
}
return true;
}
console.log(isSameOptimized([1, 2, 4, 5], [1, 4, 5, 2]))