I am going to design a method in Java which receives 2D array and find the most repetitive value for each column. So the output for this method is a one Dimensional array which contains the most repeated value for each column in the 2 D array.
It can be summarised like that,
- Count the repetitive values for each column.
- save these values in one array where each value in the output array represent the most repeated values in the 2 D array column
This is my code, I start with that
static int choseAction(int[][] Actions, int ColNumber) {
int action = 0;
int c = 0;
int d = 0;
int n = 0;
for (int i = 0; i < Actions.length; i++) {
for (int j = 0; j < Actions[0].length; j++) {
if (Actions[ColNumber][i] == 1) {
c = +1;
} else if (Actions[ColNumber][i] == -1) {
d = +1;
}
else if (Actions[ColNumber][i] == 0) {
n = +1;
}
}
}
action = ActionCompare(c, d, n);
return action;
}
static int ActionCompare(int a, int b, int c) {
int r;
if ((a > b) && (a > c)) {
r = a;
System.out.println("\n cc ");
} else if ((b > a) && (b > c)) {
r = b;
System.out.println("\n dd ");
} else {
r = c;
System.out.println("\n do nn ");
}
return r;
}
My question is that , what is the easier way to do that ?