# Java homework question

• 10-09-2013, 12:22 PM
Mavor
Java homework question
Had this assignment in my programming class last week, and I couldn't figure out how to finish it.

The assignment:
Write a method that takes in an array of integers and counts how many different numbers appear more than once.
For example: Given the array { 1; 1; 1; 2; 4; 4; 5; 6; 6 }  the answer would be 3, because three different numbers (1,4 and 6) appear more than once.

So far I've written the following code:
Code:

```public static int checkRepetition(int[] a)         {                 int repetitionCount = 0;                         for(int i=0; i<a.length; i++) //for any element in array a[]                 {                         for(int j=0; j<i; j++) //check if any previous element has the same value                         {                                 if(a[i] == a[j])                                 {                                                                                        repetitionCount++;                                        }                         }                                        }                                return repetitionCount;                }```
Which, as you might tell, goes through every element in array a[] and checks if any previous element has the same value as a[i].
The obvious problem with this is that if any number appears more than two times it will be counted more than once, so for the array { 1; 1; 1; 2; 4; 4; 5; 6; 6 } I'd get the answer 4, because {1} appears three times and therefore is counted twice as a repetition.
I'm sure there is a relatively simple way of doing this, but I have tried a few ways and haven't been able to figure it out yet so any help is greatly appreciated

Mavor
• 10-09-2013, 12:25 PM
PhHein
Re: Java homework question
ok, how would you do it with pencil and paper? Try to break it down into small steps.
• 10-09-2013, 12:47 PM
Mavor
Re: Java homework question
For every element in the array I would check if any previous element in the array has the same value (is value the right word? what I mean is what integer it contains) as the element I am checking
If I found a match I would store the value somewhere and then every time I found another match I would want to check if the value has appeared before as a match. If it has appeared as a match before then I wouldn't raise my count variable, and if it hasn't appeared as a match before then I'd raise my count variable.
Say that I'm working with the n-th element in the array and the value of that element is 23, I would first check if 23 has appeared more than once before (and hence is stored somewhere as an already matched value), if not then I'd check every previous element to see if any element also has the value 23

Hope this is close to what you were asking for. I do realise what's missing in my code, some way of knowing if a number has already appeared at least twice before, to make sure it isn't counted as a repetition twice (or more often)

EDIT: The array is sorted, which I wasn't aware of before (or actually I ignored it because I didn't know what it meant, although it should be pretty obvious).
I'm trying to solve this now with my new found info, will update soon
• 10-09-2013, 01:14 PM
Mavor
Re: Java homework question
This is what I have right now. The code works and gives the right answer but it probably could be more practical.
I compare the 1st and 2nd element in the array separately, because in the for loop I compare a[i] with a[i-2] and therefore need to start at the 3rd array to avoid going out of bounds

Code:

```        public static int checkRepetition(int[] a)         {                 int repetitionCount = 0;                                 if(a[1] == a[0]) repetitionCount++;                         for(int i=2; i<a.length; i++)                 {                         if(a[i] != a[i-2] && a[i] == a[i-1])                         {                                                                                        repetitionCount++;                                                                }                 }                        return repetitionCount;                        }```
• 10-09-2013, 02:18 PM
PhHein
Re: Java homework question
Try your solution with an unsorted array or an array having the same digit four or more times.