0

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 ?

ibra
  • 11
  • 2

2 Answers2

0

You can sum all the values in column so you can have if 1s are more or -1s but you cant tell about zeros. Do the thing what you did before for zeros and your code becomes 2/3 of full length.

public class CounterPro {
    int pn = 0; // positive numbers
    int mn = 0; // minus numbers
    int nn = 0;  // neutral numbers
    int sum = 0; // sum
    String r; // result
    int i = 0;
public String sumAll(int[] array){
while(i < array.length){
    sum += array[i];
    if(array[i]==0){
    nn += 1;
    }    
    i+=1;
}
   pn = (array.length - nn + sum)/2;
   mn = (array.length - sum -nn)/2;
   if(pn > mn && pn > nn){
   r = "woaa lost of positive 1";
   }
   else if(mn > pn && mn > nn){
   r = "woaa lost of minus 1";
   }
   else {
   r = "woaa lost of neutral numbers";
   }
   return r;
}
  • Thank you very much but i am talking about 2 D array not 1D – ibra Jul 27 '15 at 14:45
  • u can arrange it that way u are using 1D actually because you get column number so you dont have to worry abour the other dimension i didnt have time to write like 2D. – Ali yasar Erdogan Jul 27 '15 at 19:32
0

Here is an approach from the answer here

Use a HashMap<Integer, Integer>

For multiple occurrences, increment the corresponding value of the 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++){
    for (int i : Actions[j]) {
        Integer count = map.get(i);
        map.put(i, count != null ? count+1 : 0);
    }

Then append the number with maximum frequency from hash map to 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 at the end:

return output;

That's it!

Community
  • 1
  • 1
  • Thank you for your answer but i will receive 2D array in my method so your solution cannot work with my method – ibra Jul 27 '15 at 14:07
  • `Actions` is the 2D Array... I have it above –  Jul 27 '15 at 14:08
  • Can you see the 2D Array as an argument in the answer? It seems like a bit of a waste if you can't. –  Jul 27 '15 at 18:27