Skip to content

Instantly share code, notes, and snippets.

@elliotchance
Created September 11, 2013 04:37
Show Gist options
  • Save elliotchance/6519395 to your computer and use it in GitHub Desktop.
Save elliotchance/6519395 to your computer and use it in GitHub Desktop.
public boolean compareLists(int[] list1, int[] list2, int allowedFailures = 1)
{
if(list1.length != list2.length) {
return false;
}
int len = list1.length;
boolean[] checklist = new boolean[len];
int allowedFailures = 1, encounteredFailures = 0;
// cycle through the second list
for(int i = 0; i < len; ++i) {
// try and find this value on the first list to mark it off
boolean success = false;
for(int j = 0; j < len; ++j) {
if(list1[i] == list2[j] && !checklist[i]) {
// found, mark it off and continue
checklist[i] = true;
success = true;
break;
}
}
if(!success) {
// the item on the list cannot be found on the original list
++encounteredFailures;
if(encounteredFailures > allowedFailures) {
return false;
}
}
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment