-
Notifications
You must be signed in to change notification settings - Fork 70
Description
_public static boolean arePermutations(int[] a, int[] b){
for(int i = 0; i < a.length; i++){
boolean test1 = false;
for(int k = 0; k < b.length; k++){
if(a[i] == b[k]){
test1 = true;
}
}
if(test1){
return true;
}
}
return false;
}_
This method looks good at a glance, but if you test it out, it will return true, even if there is only 1 of the same numbers. It also does not check to make sure the arrays are the same length. These are pretty major issues, which i have solved. Another problem is that this method does not conform to AP requirements. AP requires you to used "enhanced for" loops (known as for-each in C#). Some more strict professors may be looking for something more like this:
public static boolean arePermutations(int[] a, int[] b){
if(a.length != b.length)
return false;
for(int i: a){
boolean test1 = false;
for(int k: b){
if(i == k)
test1 = true;
}
if(!test1)
return false;
}
return true;
}
This version will return false, immediately, if it finds an integer in the first array, that is not in the second array. It also makes sure they are the same length, which was directly specified in the question.