Skip to content

Instantly share code, notes, and snippets.

@MacTanomsup
Created November 17, 2018 05:40
Show Gist options
  • Select an option

  • Save MacTanomsup/b5de0188fc7424bb687a52ce08b5b5b3 to your computer and use it in GitHub Desktop.

Select an option

Save MacTanomsup/b5de0188fc7424bb687a52ce08b5b5b3 to your computer and use it in GitHub Desktop.
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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment