Results 1 to 5 of 5
  1. #1
    Mavor is offline Member
    Join Date
    Oct 2013
    Posts
    3
    Rep Power
    0

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

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default 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 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    Mavor is offline Member
    Join Date
    Oct 2013
    Posts
    3
    Rep Power
    0

    Default 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
    Last edited by Mavor; 10-09-2013 at 12:53 PM.

  4. #4
    Mavor is offline Member
    Join Date
    Oct 2013
    Posts
    3
    Rep Power
    0

    Default 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


    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[i-2] && a[i] == a[i-1])
    			{							
    			         repetitionCount++;					
    			}
    		}	
    		return repetitionCount;		
    	}
    Last edited by Mavor; 10-09-2013 at 01:39 PM.

  5. #5
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default 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 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

Similar Threads

  1. Java Homework Help Please!
    By xletmelive267x in forum New To Java
    Replies: 1
    Last Post: 04-15-2013, 04:36 AM
  2. new to java: homework question
    By pytho in forum New To Java
    Replies: 6
    Last Post: 06-24-2011, 06:31 AM
  3. java homework help
    By jenniferrlie in forum New To Java
    Replies: 5
    Last Post: 09-22-2009, 08:12 PM
  4. Java homework please
    By Indulgence in forum New To Java
    Replies: 1
    Last Post: 11-03-2008, 02:48 AM
  5. LF: Homework help with Java
    By excurssion in forum New To Java
    Replies: 2
    Last Post: 10-17-2008, 06:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •