0

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.

6
  • 4
    I don't think "consecutive" means what you think it means. Commented Nov 13, 2019 at 15:23
  • If you can solve the problem using a for loop, you could easily convert it to a while loop. Commented Nov 13, 2019 at 15:24
  • what happens if you have 3 of a number? Does that still count as 1 case? Commented Nov 13, 2019 at 15:24
  • Does this answer your question? Counting repeated elements in an integer array Commented Nov 13, 2019 at 15:28
  • There are many ways to do it. Can you tell the range of values which array elements can have? Commented Nov 13, 2019 at 16:57

3 Answers 3

1

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Note that this function has the side effect of rearranging the array passed as parameter. If this is undesirable, then you can make a copy of the original array and then pass that copy as parameter.
1

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   

Comments

0

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);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.