• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to find duplicate values in ArrayList with out iterarting..?

 
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All

I need to find duplicate values in ArrayList is there any method to find duplicate values..?

Example

List list = new ArrayList();
list.add("a");
list.add("a");
list.add("b");
list.add("c");
list.add("c");

Here a and c are duplicate values.

i need the output as [a,c]

I already done using set but i need Any API to solve

*i need not use any iteration logic to find just i need any API to find*




 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

You can pass the list reference to a SET and then you can iterate the values to avoid the duplicates. Also you can check the duplicates using ".contains()" but the thing is you need to check it using if/else everytime.

So my idea is better to pass the reference to SET

Regards,
Guna
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Guna sekar wrote:Hi,

You can pass the list reference to a SET and then you can iterate the values to avoid the duplicates. Also you can check the duplicates using ".contains()" but the thing is you need to check it using if/else everytime.

So my idea is better to pass the reference to SET

Regards,
Guna



Hi,
As said by Guna Sekar,
Below is example for same :-



This will return you unique list of object .
 
Sheriff
Posts: 22780
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But that includes b. The trick is to get all unique elements that occur at least two times in the list. I don't think this will be possible without iterating. Well, it isn't, if you count the API method implementations. Even methods like Collection.addAll use iterations.
 
This is my favorite show. And this is my favorite tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic