Results 1 to 6 of 6
  1. #1
    javaman1 is offline Member
    Join Date
    Sep 2010
    Posts
    56
    Rep Power
    0

    Default Array Help - Finding and replacing duplicate cells

    This method is designed to check an integer array for duplicates. It will compile and run but the logic is flawed somewhere.

    The method is designed to find duplicates and replace them with another random number, and continue until no more duplicates are found.

    Java Code:
    public static void checkDuplicates(int[] a)
    	{
    		int[] num = new int[a.length + 1];
    		boolean done = false;
    
    		for(int e = 0; e < a.length; e++)
    		{
    			num[a[e]]++;
    		}
    
    		while(done)
    		{
    			done = false;
    
    			for(int e = 0; e < a.length; e++)
    			{
    				num[a[e]]++;
    			}
    
    			for(int e = 0; e < num.length - 1; e++)
    			{
    				if(num[e] == 2)
    				{
    					num[e]--;
    					int index = search(a, e);
    					a[index] = gen.nextInt(25) + 1;
    					done = true;
    				}
    			}
    
    			for(int e = 0; e < num.length; e++)
    			{
    				num[e] = 0;
    			}
    		}
    	}
    
    	public static int search(int[] a, int value)
    	{
    		int e;
    
    		for(e = 0; e < a.length; e++)
    		{
    			if(a[e] == value)
    				break;
    		}
    
    		return e;
    	}
    Thanks in advance for the help!

    AB
    Last edited by javaman1; 02-05-2011 at 03:33 AM.

  2. #2
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    What is your input?
    What is the expected output?
    and what is the actual output?

  3. #3
    javaman1 is offline Member
    Join Date
    Sep 2010
    Posts
    56
    Rep Power
    0

    Default

    The input is a 25 cell array with random numbers up to 25.
    The expected output is the numbers 1 through 25. NO duplicates.
    The actual output does not change after the method is run. It is the same as the input.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    Java Code:
    boolean done = false;
    
    for(int e = 0; e < a.length; e++)
    {
        num[a[e]]++;
    }
    
    while(done)
    {
        // etc...

    That can't be right. done is false so the while loop will never get executed.

  5. #5
    time2die is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default

    for(int e = 0; e < a.length; e++)
    {
    num[a[e]]++;
    }
    this code is not correct, you can get OutOfArraySizeException

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    you can get OutOfArraySizeException

    Do you mean ArrayIndexOutOfBoundsException? That won't happen because the OP has declared

    Java Code:
    int[] num = new int[a.length + 1];

    and stated later that a.length is 25. So the array has 26 positions (indexed as 0 to 25).

Similar Threads

  1. How to detect duplicate values in an Array?
    By maz09 in forum New To Java
    Replies: 1
    Last Post: 04-08-2010, 08:58 AM
  2. Error if array contains duplicate integers
    By lithium002 in forum New To Java
    Replies: 4
    Last Post: 12-05-2009, 09:58 AM
  3. Counting Duplicate Variables in an Array
    By Npcomplete in forum New To Java
    Replies: 2
    Last Post: 10-24-2008, 08:33 PM
  4. [SOLVED] regexs not finding or replacing
    By Nicholas Jordan in forum Advanced Java
    Replies: 7
    Last Post: 07-30-2008, 11:02 PM
  5. replacing array values
    By Jononomous in forum New To Java
    Replies: 1
    Last Post: 05-22-2008, 04:27 PM

Tags for this Thread

Posting Permissions

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