Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

273
Visualizações
How can i Know the frecuency that a number appears in an array

I handle to save a list of random numbers in an array , now I have an array of 7 numbers : ( 3,4,6,6,10,3,5), and I want to know whats the frequency that the numbers appears .

I tried with this , but idk really how to do it

int size = 7
int aux [] = {3,4,6,6,10,3,5};
for(int i = 0 ; i<size; i++) {
            int cont = 0 ;
            for(int j = 0; i<size-1; j++) {
                if(aux[i] == aux[j+1]) {
                     aux[j+1] = -1;
                     cont++;
                } 
                
            }
            System.out.println("The number "+ aux[i] + " appears "+ cont + "times");
        }
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

You could use a Map and count the occurrences yourself in a loop:

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class Main {
    private static Map<Integer, Integer> getCounts(int[] arr) {
        Map<Integer, Integer> counts = new HashMap<>();
        for (int x : arr) {
            counts.put(x, counts.getOrDefault(x, 0) + 1);
        }
        return counts;
    }

    public static void main(String[] args) {
        int[] arr = {3, 4, 6, 6, 10, 3, 5};
        System.out.println(Arrays.toString(arr));
        Map<Integer, Integer> counts = getCounts(arr);
        for (Map.Entry<Integer, Integer> entry : counts.entrySet()) {
            System.out.printf("%d occurs %d %s\n", entry.getKey(), 
                entry.getValue(), entry.getValue() == 1 ? "time" : "times");
        }
    }
}

Output:

[3, 4, 6, 6, 10, 3, 5]
3 occurs 2 times
4 occurs 1 time
5 occurs 1 time
6 occurs 2 times
10 occurs 1 time
over 4 years ago · Santiago Trujillo Relatório

0

create a map struct Map<Integer, Integer> with key is your element, value is the time appear of element in the array.

    Map<Integer, Integer> map = new HashMap<>();
    for (int e : array){
        if (map.containsKey(e)){
            map.put(e, map.get(e) + 1);
        } else {
            map.put(e, 1);
        }
    }
    for (Map.Entry<Integer, Integer> e : map.entrySet()){
        System.out.println( e.getKey() + ": " + e.getValue());
    }
over 4 years ago · Santiago Trujillo Relatório

0

You can try with this:

import java.util.*;

public class ArrayCount {

    public static void main(String[] args) {
        int [] a = {3,4,6,6,10,3,5};
        int n = a.length;
        int [] tmp = new int [n];
        System.arraycopy(a, 0, tmp, 0, n); 
        int Num = 1;
        Arrays.sort(tmp);
        for(int i = 1; i < n; i ++) {
            if (tmp[i] != tmp[i-1]) {
                Num ++;
            }
        }
        int [] b1 = new int [Num];      
        int [] b2 = new int [Num];
        for (int i = 0; i < Num; i ++) {
            b2[i] = 1;
        }
        int j = 0;
        for(int i = 1; i < n; i ++) {
            if (tmp[i] == tmp[i-1]) {
                b1[j] = tmp[i];
                b2[j] ++;
            }
            else {
                j ++;
                b1[j] = tmp[i];
            }
        }
        System.out.println("The number of elements in the array: " + Num);
        System.out.println("List of different elements: " + b1);
        System.out.println("List of times of different elements: " + b2);
    }
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda