• Post Reply Bookmark Topic Watch Topic
  • New Topic

best way to find duplicates on array  RSS feed

 
Denis Yuzvyk
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Is there any "correct" way to find duplicates at array.



Thanks
 
Sanjeev Singh
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Map contains a key-value pair,the above is a imporper use of Map.
It is not sure from your post whether you wants to check for a duplicacy or you want the array to have only unique elements.
There can be various ways to find a duplicate elements in arrays.For the second type of problem I have a simpler solution.
List li<String> = Arrays.asList(arr);
Set se= new HashSet(li);
String str [] = se.toArray(new String[0]);
Now the str contains only unique elements.
 
Denis Yuzvyk
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tanks Sanjeev Singh.
I had second type of question.
But how to detect it? I need throw exeption if duplicates detected.
[ January 19, 2007: Message edited by: Denis Yuzvyk ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the array contains duplicates, the set will contain fewer elements than the array...
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that Set's add method returns a boolean depending on whether the Set already contains the element.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!