Java Array Element Get getFrequentElement(int[] bcp)

Here you can find the source of getFrequentElement(int[] bcp)

Description

get Frequent Element

License

Apache License

Declaration

public static int getFrequentElement(int[] bcp) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

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);//from   www .  j av a  2s  .  c  om
                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);
            }
        }
    }
}

Related

  1. getArgsOption(String[] args, String option)
  2. getArrayNoNull(Object[] array)
  3. getArrayPreview(final Object[] array, final int previewSize)
  4. getArrays(byte[] inputArray, int arraySize, boolean zeroPad)
  5. getArraySubset(T[] array, int start, int end)
  6. getFrequentElement(int[] bcp)
  7. getItems(T[] items, int[] indices)
  8. getlast(final T[] array)
  9. getLast(T[] l)