Results 1 to 2 of 2
- 11-25-2011, 03:03 AM #1
Member
- Join Date
- Sep 2009
- Posts
- 35
- Rep Power
- 0
remove duplicates in array gives wrong result
I have a char array, and in this array I want to remove repeating element I try below code unfortunately there is something wrong, it removes duplicates but it also duplicate last element. Array elements are 'A','C','C','D' but after I applied blow code I got 'A','C','D','D' on result, but I should get 'A','C','D' . can you look at the code and tell me the problem
Java Code:int NewLength = 1; int i,j; for(i=1; i< LHSArray.length; i++) { for(j=0; j< NewLength ; j++) { if(LHSArray[i] == LHSArray[j]) break; } if (j==NewLength ) LHSArray[NewLength++] = LHSArray[i]; }
- 11-25-2011, 03:31 AM #2
Re: remove duplicates in array gives wrong result
Remember that arrays have a fixed length. You cannot "chop off" the last element. you have 2 options: keep a counter that holds the number of desired elements in the array and only print out that many values. Or make a new smaller array and copy across only the desired elements.
Similar Threads
-
Remove Duplicates in Array
By HSKrustofsky in forum New To JavaReplies: 11Last Post: 09-06-2011, 05:47 AM -
Remove duplicates in 2D array
By lakshmibvaraprasad in forum New To JavaReplies: 5Last Post: 07-22-2011, 08:49 PM -
New To Java - Input Number Won't Prompt, Wrong Result
By jhuwalker in forum New To JavaReplies: 2Last Post: 01-31-2011, 10:29 PM -
Duplicates in String Array
By turnergirl24 in forum New To JavaReplies: 1Last Post: 11-04-2009, 11:09 PM -
FInd the no. of duplicates in an array
By singularity in forum Advanced JavaReplies: 3Last Post: 09-04-2009, 09:25 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks