Skip to content

Instantly share code, notes, and snippets.

@karlaoh99
Created July 27, 2022 05:15
Show Gist options
  • Save karlaoh99/c675fd578a0995e9fd5983c2e60ded1b to your computer and use it in GitHub Desktop.
Save karlaoh99/c675fd578a0995e9fd5983c2e60ded1b to your computer and use it in GitHub Desktop.
/*
Given two arrays A and B, return the indices at
which the two arrays intersect. If the two arrays
have no intersection at all, return null.
*/
function findIntersection(listA, listB) {
let iA = listA.length - 1;
let iB = listB.length - 1;
while (iA >= 0 && iB >= 0) {
if (listA[iA] != listB[iB])
break;
iA--;
iB--;
}
if (iA == listA.length - 1) return null;
return [iA + 1, iB + 1];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment