Please do not provide outright answers, rather give hints, suggestions, etc. as to how to solve the problem. Learning is more effective this way. Thanks!
You really had me searching for an answers variable...
The java collection tutorials show some tricks to remove duplicates.
And how iterators work and such, for when you need to show you can manually remove duplicates.
Rajendra Prakash
Ranch Hand
Joined: Sep 10, 2009
Posts: 293
posted
0
can you correct 2 Errors in the following code
1.what parameter should be passed in RemoveDuplicates().
2.Incompatible types : return ret
Ronald de Gier
Greenhorn
Joined: Sep 09, 2009
Posts: 9
posted
0
Rajendra Prakash wrote: can you correct 2 Errors in the following code
1.what parameter should be passed in RemoveDuplicates().
2.Incompatible types : return ret
What are you using parameter list for? It doesn't seem to be used in the function.
What are your thoughts on the code so far?
Rajendra Prakash
Ranch Hand
Joined: Sep 10, 2009
Posts: 293
posted
0
I passed list object in the RemoveDuplicate(), but compiler error.
return statement also throws compiler error
Well, you're not returning the type you say you are. And you're checking every item in the list you just created against the list you just created, so every element is pretty much guaranteed to be in the list... because you put it there. And if it's not there (impossible), you put it back into the same list.
I suspect what you meant to do was check every item in the list you pass in (which you don't do) against the return list--maybe head down that road and post a followup with your new code.
Can I suggest that you check if the element is in the list before you add it? Collections have a contains method which returns a boolean as to whether an element exists. Can I suggest you use the contains method to decide whether or not an item is already in the list and thus whether or not you have to add the item..
Edit: Doh missed the above post, sorry guys.
Sean
I love this place!
Mukesh Ranjan
Greenhorn
Joined: Jun 24, 2009
Posts: 22
posted
0
It is almost a bad idea to remove duplicate and using loops for that.
Its better to use hashset then, below is the example code;
David Newton wrote:Please do not provide outright answers, rather give hints, suggestions, etc. as to how to solve the problem. Learning is more effective this way. Thanks!
And when you *do* violate the spirit of JavaRanch, at least UseCodeTags.
Mukesh Ranjan
Greenhorn
Joined: Jun 24, 2009
Posts: 22
posted
0
Agreed !!
Vigneshwar Subramanian
Greenhorn
Joined: Feb 28, 2013
Posts: 1
posted
0
Rajendra Prakash wrote:Correct this code to remove duplicates in arraylist1
After printing 'al2' we get a list with duplicates .... wat will be the problem i detected tat problem is with if(!al2.equals(al1)) statement but i could not recognize wats d problem .. if v remove
the not symbol '!' then the control isn't not coming inside the for loop itself
@vigneshwar- dude have you hear of bubble sort algorithm? it checks the first value with each an every value of array/list.
try doing with that and post what happens.