1

Here I have sample java code which to count number of duplicate numbers in an array.

example:

I have array A[]={3,4,5,3,4,3} so I need the output

3 occurred 3 times
4 occurred 2 times 
5 occurred 1 time

How can I get this with my following program.

public class testNumberCount {

    public static void main(String[] args) {
        String[] temp;
        System.out.println("Enter numbers Separated with comma(,)");
        String inputSrc = "";
        try {
            Scanner sc = new Scanner(System.in);
            inputSrc = sc.nextLine();
        } catch (Exception ex) {
            System.out.println("Exception:" + ex.getMessage());
        }
        temp = inputSrc.split(",");
        System.out.println(Arrays.toString(temp));
        int[] numberarray = new int[temp.length];
        for (int i = 0; i < temp.length; i++) {
            numberarray[i] = Integer.parseInt(temp[i]);
        }
        for (int j = 0; j < numberarray.length; j++) {
            for (int k = 0, count = 0; k < numberarray.length; k++) {
                if (numberarray[j] == numberarray[k]) {
                    count++;
                }
                System.out.println("number " + numberarray[k] + " Occured:" + count + " times.");
            }
        }
    }
}

here my wrong output is:

number 3 Occured:1 times.
number 4 Occured:1 times.
number 5 Occured:1 times.
number 3 Occured:2 times.
number 4 Occured:2 times.
number 3 Occured:3 times.
number 3 Occured:0 times.
number 4 Occured:1 times.
number 5 Occured:1 times.
number 3 Occured:1 times.
number 4 Occured:2 times.
number 3 Occured:2 times.
number 3 Occured:0 times.
number 4 Occured:0 times.
number 5 Occured:1 times.
number 3 Occured:1 times.
number 4 Occured:1 times.
number 3 Occured:1 times.
number 3 Occured:1 times.
number 4 Occured:1 times.
number 5 Occured:1 times.
number 3 Occured:2 times.
number 4 Occured:2 times.
number 3 Occured:3 times.
number 3 Occured:0 times.
number 4 Occured:1 times.
number 5 Occured:1 times.
number 3 Occured:1 times.
number 4 Occured:2 times.
number 3 Occured:2 times.
number 3 Occured:1 times.
number 4 Occured:1 times.
number 5 Occured:1 times.
number 3 Occured:2 times.
number 4 Occured:2 times.
number 3 Occured:3 times.

For this one I'm getting some wrong output, can anybody run and solve my problem.

Your help will be appreciated.

5
  • 1
    First thing to improve in your sample code: replace the first two thirds of it with int[] numberArray = { 3, 4, 5, 3, 4, 3 };. Then you can focus on the important bit which is going wrong. Now, what is your actual output? Commented Nov 20, 2014 at 7:44
  • Use Map instead of Array that need to resize. Commented Nov 20, 2014 at 7:46
  • @WundwinBorn,can you modify my code please. Commented Nov 20, 2014 at 7:49
  • @JonSkeet,I'm getting output as: Commented Nov 20, 2014 at 7:49
  • Don't put it in a comment - add it to the question. I've given you a suggestion for improved output, and a hint for how to go further. Commented Nov 20, 2014 at 7:50

1 Answer 1

2

You're currently writing output on each iteration of the inner loop... which means you're getting much more output than you want. You want to print out the result once per iteration of the outer loop, which means bringing count into the scope of that loop too, and not using k in the output either. So here's a start:

class Test {
   public static void main(String[] args) throws Exception {
       int[] numberarray = { 3, 4, 5, 3, 4, 3 };
       for (int j = 0; j < numberarray.length; j++) {
           int count = 0;
           for (int k = 0; k < numberarray.length; k++) {
               if (numberarray[j] == numberarray[k]) {
                   count++;
               }
           }
           System.out.println("Number " + numberarray[j]
                              + " occurred " + count + " times.");
       }
   }
}

The output of this is:

Number 3 occurred 3 times.
Number 4 occurred 2 times.
Number 5 occurred 1 times.
Number 3 occurred 3 times.
Number 4 occurred 2 times.
Number 3 occurred 3 times.

Now, that's accurate - but includes duplicates. Think about how you can detect duplicates (think about the relationship between j and k when you've spotted that numberarray[j] == numberarray[k]).

An alternative is to go through the array once, maintaining a Map<Integer, Integer> where the key is the value in the array, and the value is the number of times you've seen that value... but you might want to get the brute force version working first.

Sign up to request clarification or add additional context in comments.

4 Comments

Jon Skeet,Can you check my updated post,i mentioned coming output.
Jon Skeet,I'm learner i dont have that much idea can you please update your code using Map or something easy way.
@GangadharB: No, I'm suggesting that you don't go with the Map approach for the moment, but get your current approach working properly. Take the code I've written, and then think about how you can spot that you've already written the output for a number. I've given you a big hint in the post. (I'm assuming you're doing all of this as a school exercise, so it's important that you think and learn rather than just getting the whole answer from me directly.)
@GangadharB: Well that doesn't tell us anything about what you tried, what debugging you've done, what the output was, etc.

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.