Skip to content

Instantly share code, notes, and snippets.

@sryze
Created September 23, 2019 11:42
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 sryze/749b4857cb15092c0c71798a9527d5df to your computer and use it in GitHub Desktop.
Save sryze/749b4857cb15092c0c71798a9527d5df to your computer and use it in GitHub Desktop.
function mergeArraysKeepOrder(a1, a2) {
var a = [];
var i = 0, j = 0;
while (true) {
for (; i < a1.length; i++) {
if (a2.indexOf(a1[i], j) != -1) {
i++;
break;
}
a.push(a1[i]);
}
for (; j < a2.length; j++) {
if (a1.indexOf(a2[j], i) != -1) {
j++;
break;
}
a.push(a2[j]);
}
if (i == a1.length && j == a2.length) {
break;
}
}
return a;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment