design a method in Java which receives 2D array and find the most repetitive value for each column -
i going design method in java receives 2d array , find repetitive value each column. output method 1 dimensional array contains repeated value each column in 2 d array.
it can summarised that,
- count repetitive values each column.
- save these values in 1 array each value in output array represent repeated values in 2 d array column
this code, start
static int choseaction(int[][] actions, int colnumber) { int action = 0; int c = 0; int d = 0; int n = 0; (int = 0; < actions.length; i++) { (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 nn "); } return r; }
my question , easier way ?
here approach answer here
use hashmap<integer, integer>
for multiple occurrences, increment corresponding value of integer key;
public int[] static getfrequencies(int[][] actions){
int[] output = new int[actions.length] map<integer, integer> map = new hashmap<integer, integer>(); for(int j = 0; j < actions.length; j++){ (int : actions[j]) { integer count = map.get(i); map.put(i, count != null ? count+1 : 0); }
then append number maximum frequency hash map output array:
output[j] = collections.max(map.entryset(), new comparator<map.entry<integer, integer>>() { @override public int compare(entry<integer, integer> o1, entry<integer, integer> o2) { return o1.getvalue().compareto(o2.getvalue()); } }).getkey().intvalue(); }
and return output @ end:
return output;
that's it!
Comments
Post a Comment