Skip to content

Instantly share code, notes, and snippets.

@emersxw
Created July 12, 2020 15:47
Show Gist options
  • Save emersxw/fc2911f1d2306502c8456c5276e19a89 to your computer and use it in GitHub Desktop.
Save emersxw/fc2911f1d2306502c8456c5276e19a89 to your computer and use it in GitHub Desktop.
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;
}
console.log(arr2);
arr2.splice(correctIndex,1)
}
return true;
}
same([1,2,3,2], [9,1,4,4])
function same2(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
}
console.log(frequencyCounter1);
console.log(frequencyCounter2);
for(let key in frequencyCounter1){
if(!(key ** 2 in frequencyCounter2)){
return false
}
if(frequencyCounter2[key ** 2] !== frequencyCounter1[key]){
return false
}
}
return true
}
same2([1,2,3,2,5], [9,1,4,4,11])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment