Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.ArrayList;

import java.util.HashMap;

public class Main {
    public static int getFrequentElement(int[] bcp) {
        HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
        ArrayList<Integer> count = new ArrayList<Integer>();
        ArrayList<Integer> uniId = new ArrayList<Integer>();
        int id = 0;

        for (int col = 0; col < bcp.length; col++) {
            //System.out.print(bcp[col] + "\t");
            int no = 0;
            if (!map.containsKey(bcp[col])) {
                map.put(bcp[col], id++);
                count.add(1);
                uniId.add(bcp[col]);
            } else {
                no = map.get(bcp[col]);
                count.set(no, count.get(no) + 1);
            }
        }

        int maximum = Integer.MIN_VALUE;
        int maxId = Integer.MIN_VALUE;
        for (int i = 0; i < count.size(); i++) {
            //System.out.print(uniId.get(i) + ":" + count.get(i) + ",\t");
            if (maximum < count.get(i)) {
                maximum = count.get(i);
                maxId = uniId.get(i);
            }
        }
        //System.out.println();

        map.clear();
        uniId.clear();
        count.clear();
        return maxId;
    }

    public static void getFrequentElement(int[][] bcp, int[] res, char flag) {
        if (flag == 'r') {
            for (int row = 0; row < bcp.length; row++) {
                res[row] = getFrequentElement(bcp[row]);
            }
        } else {
            int colL = bcp[0].length;
            int[] column = new int[bcp.length];
            for (int col = 0; col < colL; col++) {
                for (int row = 0; row < bcp.length; row++) {
                    column[row] = bcp[row][col];
                }
                res[col] = getFrequentElement(column);
            }
        }
    }
}