This is for JAVA programming, by the way.
So, I am trying to code a method where it will return the most frequent int or ints in an array. I already have a whole class set up to find average, sum, and etc., but this specific method is a bit tricky. My code so far is this:
public int[] modeOfData()
{
List<Integer> modes = new ArrayList<Integer>( );
int maxCount=0;
for (int i = 0; i < data.length; ++i){
int count = 0;
for (int j = 0; j < data.length; ++j){
if (data[j] == data[i]) ++count;
}
if (count > maxCount){
maxCount = count;
modes.clear();
modes.add( data[i] );
} else if ( count == maxCount ){
modes.add( data[i] );
}
}
return modes.toArray( new Integer[modes.size()] );
}
The return part of this code is the only part that has a syntax error. The syntax error reads: incompatible types. It highlights the part in parentheses. What am I doing wrong, and what must I edit? Thanks! Is there an easier way to code this with just if-elses and loops? Or a way without lists? Thanks again.
public Integer[] modeOfData()would be much appropiate