File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Remove duplicates in arraylist Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Remove duplicates in arraylist" Watch "Remove duplicates in arraylist" New topic
Author

Remove duplicates in arraylist

Rajendra Prakash
Ranch Hand

Joined: Sep 10, 2009
Posts: 293
Correct this code to remove duplicates in arraylist1

N.Senthil Kumar
Ranch Hand

Joined: Jul 07, 2009
Posts: 50
Hi Raj,

Why don't you use Set instead of List, if you don't need duplicates


N.Senthil Kumar
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Correct this code to remove duplicates in arraylist1

The ranch is NotACodeMill. If you have some difficulties, tell us where you find yourself stuck at. What did you try ?

And there isn't any arraylist1 in your code...


[My Blog]
All roads lead to JavaRanch
Sunil Tiwari
Ranch Hand

Joined: Sep 19, 2006
Posts: 49

Try this! This is a very trivial solution!



~ Neil (SCJP)
Rajendra Prakash
Ranch Hand

Joined: Sep 10, 2009
Posts: 293
ok , can you give atleast logic to remove duplicates in arraylist without hashset.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16


What are you checking here ? Shouldn't you check if str2 is not already in in al2 ?
Sunil Tiwari
Ranch Hand

Joined: Sep 19, 2006
Posts: 49

Another trivial solution

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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!
Sunil Tiwari
Ranch Hand

Joined: Sep 19, 2006
Posts: 49

Sure David!
Ronald de Gier
Greenhorn

Joined: Sep 09, 2009
Posts: 9
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
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
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
I passed list object in the RemoveDuplicate(), but compiler error.
return statement also throws compiler error
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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.
Sean Clark
Rancher

Joined: Jul 15, 2009
Posts: 377

Hey,

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

ArrayList<String> al1 = new ArrayList<String>();
al1.add("raja");
al1.add("raja");
al1.add("tendulkar");
al1.add("prakash");
al1.add("sachin");
al1.add("sachin");
al1.add("zaheer");
al1.add("asin");
al1.add("surya");

HashSet<String> set = new HashSet<String>(al1);

Iterator iterator = set.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}

Answer:
asin
prakash
tendulkar
surya
sachin
zaheer
raja
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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
Agreed !!
Vigneshwar Subramanian
Greenhorn

Joined: Feb 28, 2013
Posts: 1
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
Ishan Pandya
Ranch Hand

Joined: Feb 06, 2012
Posts: 223

@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.


OCPJP
Ishan Pandya
Ranch Hand

Joined: Feb 06, 2012
Posts: 223

@OP - why do you compulsory need to use List for unique elements? is there any specific requirement for that?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

Ishan Pandya wrote:@OP - why do you compulsory need to use List for unique elements? is there any specific requirement for that?

I think OP is long gone. Look at the date of his post.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Ishan Pandya
Ranch Hand

Joined: Feb 06, 2012
Posts: 223

Winston Gutkowski wrote:
I think OP is long gone. Look at the date of his post.


yes actually. he must have found out something for sure.
Surinder Mehra
Ranch Hand

Joined: Aug 06, 2013
Posts: 32
    
    1
Change the if Condition inside for loop. .equals method will add all the element of al1 to al2. !al2.contains(elemenst of al1) will add element only once. Below code might be helpful


Output:
List al1: [raja, raja, tendulkar, prakash, sachin, sachin, zaheer, asin, surya]
List al2: [raja, tendulkar, prakash, sachin, zaheer, asin, surya]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Remove duplicates in arraylist