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   w  w  w .  ja v a2s  .  c  o  m*/
                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);
            }
        }
    }

    public static short getFrequentElement(short[] bcp) {
        HashMap<Short, Short> map = new HashMap<Short, Short>();
        ArrayList<Short> count = new ArrayList<Short>();
        ArrayList<Short> uniId = new ArrayList<Short>();
        short id = 0;

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

        short maximum = Short.MIN_VALUE;
        short maxId = Short.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;
    }
}

Related

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