Skip to content

Instantly share code, notes, and snippets.

@erezLieberman
Created July 9, 2019 16:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erezLieberman/54d8a3a469c2d9881d527c28ca70e6d9 to your computer and use it in GitHub Desktop.
Save erezLieberman/54d8a3a469c2d9881d527c28ca70e6d9 to your computer and use it in GitHub Desktop.
pXqeNE
function areSimilar(a, b) {
const compare = (first,second) => first.toString() == second.toString();
if([...a].sort((a,b) => a - b).toString() !== [...b].sort((a,b) => a - b).toString()){
return false
}
if(!compare(a,b)){
for(let i = 0; i<a.length; i++){
console.log("before",b)
let placeholder = b[0]
b[0] = b[i];
b[i] = placeholder;
// b[0] = placeholder;
console.log("after",b)
if(compare(a,b)){
return true;
}
}
for(let i = 0; i<a.length; i++){
console.log("before",b)
let placeholder = b[1]
b[1] = b[i];
b[i] = placeholder;
// b[0] = placeholder;
console.log("after",b)
if(compare(a,b)){
return true;
}
}
for(let i = 0; i<a.length; i++){
console.log("before",b)
let placeholder = b[2]
b[2] = b[i];
b[i] = placeholder;
// b[0] = placeholder;
console.log("after",b)
if(compare(a,b)){
return true;
}
}
}
return compare(a,b)
}
const _a = [2, 3, 1];
const _b = [1, 3, 2];
console.log(areSimilar(_a, _b));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment