• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

Remove duplicates in arraylist

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

 
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
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • 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...
 
Ranch Hand
Posts: 49
Spring Redhat Clojure
  • 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 VI Editor Ubuntu
  • 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
Spring Redhat Clojure
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another trivial solution

 
Author
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
Spring Redhat Clojure
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure David!
 
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
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.
 
Rancher
Posts: 377
Android Spring Java
  • 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
 
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
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 !!
 
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
 
Ranch Hand
Posts: 228
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: 228
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?
 
Bartender
Posts: 10777
71
Hibernate Eclipse IDE 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: 228
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.
 
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]
 
Friends help you move. Good friends help you move bodies. This tiny ad will help:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic