Results 1 to 6 of 6
- 02-05-2011, 01:49 AM #1
Member
- Join Date
- Sep 2010
- Posts
- 56
- Rep Power
- 0
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.
Thanks in advance for the help!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; }
ABLast edited by javaman1; 02-05-2011 at 02:33 AM.
- 02-05-2011, 02:07 AM #2
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
What is your input?
What is the expected output?
and what is the actual output?
- 02-05-2011, 02:22 AM #3
Member
- Join Date
- Sep 2010
- Posts
- 56
- Rep Power
- 0
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.
- 02-05-2011, 02:35 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
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.
- 02-05-2011, 08:37 AM #5
Member
- Join Date
- Dec 2010
- Posts
- 2
- Rep Power
- 0
this code is not correct, you can get OutOfArraySizeExceptionfor(int e = 0; e < a.length; e++)
{
num[a[e]]++;
}
- 02-05-2011, 05:06 PM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Similar Threads
-
How to detect duplicate values in an Array?
By maz09 in forum New To JavaReplies: 1Last Post: 04-08-2010, 07:58 AM -
Error if array contains duplicate integers
By lithium002 in forum New To JavaReplies: 4Last Post: 12-05-2009, 08:58 AM -
Counting Duplicate Variables in an Array
By Npcomplete in forum New To JavaReplies: 2Last Post: 10-24-2008, 07:33 PM -
[SOLVED] regexs not finding or replacing
By Nicholas Jordan in forum Advanced JavaReplies: 7Last Post: 07-30-2008, 10:02 PM -
replacing array values
By Jononomous in forum New To JavaReplies: 1Last Post: 05-22-2008, 03:27 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks