By mode, I am checking if the array has duplicates. I checked similar queries down here but they addressed the question with answers containing ArrayList
and HashMap
. I am not familiar with them and trying to answer it with an array. My following code only works for 1 duplicate and unable to perform multiple duplicate detection.
public class Mode {
public static void main(String[] args) {
int[] num = {2,3,4,5,8,8,8,7,7,7};
int mode = mode(num);
System.out.println(mode);
}
public static int mode(int[] num){
for(int x=0; x < num.length; x++){
for(int y=x+1; y < num.length; y++){
if(num[x] == num[y]){
return num[x];
}
}
}
return num[0];
}
}