Results 1 to 5 of 5
Thread: Java homework question
 10092013, 12:22 PM #1Member
 Join Date
 Oct 2013
 Posts
 3
 Rep Power
 0
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:
Java 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; }
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
 10092013, 12:25 PM #2
Re: Java homework question
ok, how would you do it with pencil and paper? Try to break it down into small steps.
Math problems? Call 1800[(10x)(13i)^2][sin(xy)/2.362x]
The Ubiquitous Newbie Tips
 10092013, 12:47 PM #3Member
 Join Date
 Oct 2013
 Posts
 3
 Rep Power
 0
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 nth 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 soonLast edited by Mavor; 10092013 at 12:53 PM.
 10092013, 01:14 PM #4Member
 Join Date
 Oct 2013
 Posts
 3
 Rep Power
 0
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[i2] and therefore need to start at the 3rd array to avoid going out of bounds
Java 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[i2] && a[i] == a[i1]) { repetitionCount++; } } return repetitionCount; }
Last edited by Mavor; 10092013 at 01:39 PM.
 10092013, 02:18 PM #5
Re: Java homework question
Try your solution with an unsorted array or an array having the same digit four or more times.
Math problems? Call 1800[(10x)(13i)^2][sin(xy)/2.362x]
The Ubiquitous Newbie Tips
Similar Threads

Java Homework Help Please!
By xletmelive267x in forum New To JavaReplies: 1Last Post: 04152013, 04:36 AM 
new to java: homework question
By pytho in forum New To JavaReplies: 6Last Post: 06242011, 06:31 AM 
java homework help
By jenniferrlie in forum New To JavaReplies: 5Last Post: 09222009, 08:12 PM 
Java homework please
By Indulgence in forum New To JavaReplies: 1Last Post: 11032008, 03:48 AM 
LF: Homework help with Java
By excurssion in forum New To JavaReplies: 2Last Post: 10172008, 06:00 AM
Bookmarks