Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Remove duplicates in arraylist

 
Rajendra Prakash
Ranch Hand
Posts: 293
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct this code to remove duplicates in arraylist1

 
N.Senthil Kumar
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Raj,

Why don't you use Set instead of List, if you don't need duplicates
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Sunil Tiwari
Ranch Hand
Posts: 49
Clojure Redhat Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this! This is a very trivial solution!

 
Rajendra Prakash
Ranch Hand
Posts: 293
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok , can you give atleast logic to remove duplicates in arraylist without hashset.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

What are you checking here ? Shouldn't you check if str2 is not already in in al2 ?
 
Sunil Tiwari
Ranch Hand
Posts: 49
Clojure Redhat Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another trivial solution

 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
Clojure Redhat Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure David!
 
Ronald de Gier
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 293
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 293
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I passed list object in the RemoveDuplicate(), but compiler error.
return statement also throws compiler error
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 377
Android Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mukesh Ranjan
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agreed !!
 
Vigneshwar Subramanian
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 226
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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.
 
Ishan Pandya
Ranch Hand
Posts: 226
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@OP - why do you compulsory need to use List for unique elements? is there any specific requirement for that?
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ishan Pandya
Ranch Hand
Posts: 226
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic