Number of repetitions within an array

0

I'm trying to count how many times an element appears inside an array.

'   
    public static void main(String[] args) {        
    Scanner scan = new Scanner(System.in);
    int[] array = new int[4];
    int a;

    for(int i = 0; i < array.length; i++){
        System.out.println("Digite um numero: ");
        a = scan.nextInt();
        array[i] = a;
    }
    System.out.println("Seu array ficou: " + Arrays.toString(array));
        int vezes = 0;
        for(int i = 0; i < array.length; i++){
                for(int j = 0; j < array.length; j++){
                        if(array[i] == array[j] && i!=j){
                            int numero = array[i];
                            vezes++;
                            while(array[i]==array[j] && vezes>1){
                                System.out.println("O numero "+numero+" é repetido "+vezes+" vezes.");
                                vezes=0;
                            }
                        }
                }
        }
}'

In my example I can only make it show elements that repeat 2 times correctly, this happens because I equate times = 0 when it repeats more than once. How can I make it not fall into while(array[i]==array[j] && vezes>1) until it goes through all the repeated elements? You can use an "if" as well.

If filled with numbers 1,1,1,2 for example already wrong.

    
asked by anonymous 25.08.2016 / 03:31

1 answer

1

In order to work, you could only print after making all the postings. To do this, you would need to save everything that you accounted for and then just print the variable that stored those postings. One possible solution would be to adjust your code to the following:

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int[] array = new int[4];
    List<Integer> numerosJaContabilizados = new ArrayList<Integer>();
    Map<Integer, Integer> numeroRepeticoes = new HashMap<Integer, Integer>();
    int a;

    for (int i = 0; i < array.length; i++) {
        System.out.println("Digite um numero: ");
        a = scan.nextInt();
        array[i] = a;
    }
    System.out.println("Seu array ficou: " + Arrays.toString(array));
    for (int i = 0; i < array.length; i++) {
        for (int j = 0; j < array.length; j++) {
            if (array[i] == array[j] && i != j && !numerosJaContabilizados.contains(array[i])) {
                registrarNumeroRepeticao(array[i], numeroRepeticoes);
            }
        }
        numerosJaContabilizados.add(array[i]);
    }
    for (Entry<Integer, Integer> numeroRepeticao : numeroRepeticoes.entrySet()) {
        System.out.println(
                "O numero " + numeroRepeticao.getKey() + " é repetido " + numeroRepeticao.getValue() + " vezes.");
    }
}

private static void registrarNumeroRepeticao(int numero, Map<Integer, Integer> numeroRepeticoes) {
    if (numeroRepeticoes.get(numero) != null)
        numeroRepeticoes.put(numero, numeroRepeticoes.get(numero) + 1);
    else
        numeroRepeticoes.put(numero, 2);
}

Changes made:

  • Added a variable to store the number repetitions: numberRepeats. Note that it is a map because there you can use the key as the number and as value the repeats.
  • Added a variable to count the numbers already processed: numberAccount. Your print while would be more or less for this, but the logic would break if the repeated number did not come in sequential order. For this it is better to use a variable that stores these postings and is used to check whether the number should be counted or not.
  • Added impression of values OFF of loops. This will print the result only after processing everything.
  • This was an example of how your method would work, but here are some suggestions:

  • Refactor method: Your main method has many responsibilities, ideally it should be split between at least two different methods. For example, one responsibility could be to read the data, the other would be the counting of repetitions and the other would be the printing of the repetitions. It is an interesting exercise to try to modularize a method to the maximum. I would recommend;)
  • Use the collections API: I saw that you use arrays with primitive types but you lose the power that API collections gives you! Even using it you would solve your problem in a few lines of code, but there it would not be very didactic so it is better to try to implement the logic of the same zero.
  • I hope I have helped ^^

        
    29.08.2016 / 14:40