Created
September 11, 2013 04:37
-
-
Save elliotchance/6519395 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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