How to create a vector that displays the frequency of each element in an array?

1

"Write a Freq. method that receives data from an array A that has integers between 0 and 0, generates a vector with the occurrences of each integer between 0 and x. that takes a vector of integers and returns the largest element of the vector.Form the main module, which fills an array with only values between 0 exe and displays which value was the most frequent (call the / strong>, major and leMatrix ). "

I've already assembled the array by sorting the numbers, now I'm trying to create the FrequencyFactory method. I have two questions:

1) How do I get the algorithm to "skip" the counting of numbers that have already been counted? For example, if the matrix is: (0 0 1); (230); (1 2 3), the program will count 0 three times, then when it goes next, it will count 0 three times again.

2) How to declare the vector that will store the counted frequencies without knowing its size? I thought about creating a string and then converting it to integer vector, but repeating the frequency count would still be a problem. I also thought about leaving the array elements in ascending order to find the highest value. By this I would know the size of the vector that stores the frequencies. But I'm not able to sort using Arrays.sort () ...

public class Freq_Maior {
    static Scanner ent = new Scanner(System.in);
    public static void main(String[] args) {
        System.out.print("Digite o número de linhas e colunas da matriz: ");
        int m = ent.nextInt();
        int n = ent.nextInt();
        System.out.print("Digite o valor máximo do intervalo usado para preencher a matriz: ");
        int x = ent.nextInt();
        int [][] A = new int[m][n];
        leMatrizR(A, x);

    }
    // método para ler a matriz
    public static void leMatrizR(int matriz[][], int a) {
        Random rnd = new Random();
        for (int i=0; i<matriz.length; i++) {
            for (int j=0; j<matriz[0].length; j++) {               
                matriz[i][j] = rnd.nextInt(a+1);
            }
        }
    } 
    public static void Freq(int matriz[][]) {
        ????
    }
}
    
asked by anonymous 07.12.2014 / 19:16

1 answer

0

Your vector with the frequencies you actually know the size: x + 1 , being x the maximum value of the range used to fill the array. Then the freqs vector can be declared as:

int [] freqs = new int[x + 1];

Once you have done this, you can pass it to the Freq method and perform the frequency computation, and to "skip" the already counted elements, just compare them with the element we are currently trying to count: p>

public static void contaFrequencia(int matriz[][], int freqs[]) {
    // para cada elemento possível (0..x)...
    for ( int e = 0; e < freqs.length; e++ ) {
        freqs[e] = 0; // initializa o valor da frequência com 0.
        for ( int i = 0; i < matriz.length; i++ ) {
            for ( int j = 0; j < matriz[i].length; j++ ) {
                // se encontramos o elemento...
                if ( matriz[i][j] == e ) {
                    // incrementamos a frequência do elemento.
                    freqs[e]++;
                }
            }
        }
    }
}
    
07.12.2014 / 19:44