The code assignment is as follows:
/**
* Determine if there is a majority element in an array of ints.
* The parameter list is not altered as a result of this
* method.
* @param list != null
* @return the first index of the first occurrence of the majority element if it exists.
* If a majority element does not exist return -1.
*/
with my test cases, it keeps returning -1, it does not seem to even use the if loops. Can anyone tell me why it is doing that?
public static int findMajority(int[] list) { assert ( list != null ) : "Violation of precondition: findMajority"; int count = 0; int max = (list.length / 2) + 1; int index = -1; for ( int i = 0; i < list.length;i++){ for ( int k =list.length-1; k <= 0;i--){ if (list[i] == list[k]){ count++; if ( count >= max){ index = i; return index; } } } } return index; }