Number of repeated values in an integer array

623 views Asked by At

I need a function or method to find the number of values that repeat in one value. I am not looking for the numbers that repeat, or how many times they repeat, but simply the quantity of numbers that repeat in general. That may sound confusing so here is an example:

int[] anArray = { 
    1, 2, 3,
    4, 3, 1, 
    7, 2, 9, 1
};

The number 1 has multiple instances, so does 2 and 3.
Therefore my output should be "There are 3 cases of consecutive values"
This is what I have so far:

    int x;
    int i = 0;
    int[] array = new int[10];

    do {
      x = input.nextInt();
      array[i] = x;
      i++;
    } while(x!=0);

Outside of the loop I need to be able to output the number of repeating values. This is while loop practice, and therefore I am not allowed to use for loops.

3

There are 3 answers

1
MRosario On BEST ANSWER

If you must use while loops then you can try the following simple algorithm that makes use of the Arrays.sort function:

int getRepeatedElements(int[] array){
    //Sort the numbers in ascending order
    java.util.Arrays.sort(array);

    //Strategy: Store the first occurrence of each number and then move to the next element to see if it is repeated
    int currentElement = array[0];
    int repeatedCount = 0;
    int i = 1;
    boolean alreadyCounted = false;

    while(i < array.length){
        if(currentElement == array[i]){
            //Found a repeated element!
            if(!alreadyCounted){
                repeatedCount++;
                alreadyCounted = true;
            }
        }
        else{
            //New element found
            currentElement = array[i];
            alreadyCounted = false;
        }
        i++; //Move to the next element
    }
    return repeatedCount;
}
0
Edu Costa On

You can use the following pseudo-code:

  • Given an array of numbers
  • Group all the values by number
  • Filter all values with repetition
  • Print all the repeated number

The implementation is bellow:

        int[] anArray = { 1, 2, 3, 4, 3, 1, 7, 2, 9, 1 };
        Arrays.stream(anArray).boxed()
            .collect(Collectors.groupingBy(Integer::intValue))
            .values().stream().filter(l->l.size()>1)
            .forEach(numbers -> System.out.println(numbers.get(0)));

The output will be:

1
2
3   
0
Madhubala On

Try this:

import java.util.*;

public class Main {

    public static void main(String[] args) {
        int[] anArray = { 1, 2, 3, 4, 3, 1, 7, 2, 9, 1, 1, 2 };
        java.util.Arrays.sort(anArray);
        System.out.println(Arrays.toString(anArray));
        int countedElement = anArray[0]; // has the checked element
        int count = 0;
        for (int i = 1; i < anArray.length - 1; i++) {
            if (countedElement == anArray[i] && anArray[i] != anArray[i + 1]) {
                count++;
            } else {
                countedElement = anArray[i];
            }
        }
        System.out.println(count);
    }
}