remove duplicates?

B

bob

Let's say you have a Vector of String objects. What is the easiest
way to remove duplicates?
 
S

Stanimir Stamenkov

Mon, 5 Sep 2011 01:44:06 -0700 (PDT), /bob/:
Let's say you have a Vector of String objects. What is the easiest
way to remove duplicates?

Here what I immediately think of:

List<String> list;
...
Set<String> set = new HashSet<String>();
for (Iterator<String> iter = list.iterator();
iter.hasNext(); ) {
String str = iter.next();
if (!set.add(str)) {
iter.remove();
}
}

If you want to leave the last occurrences of the String elements you
would just iterate the list backwards:

for (ListIterator<String> iter = list.listIterator(list.size());
iter.hasPrevious(); ) {
String str = iter.previous();
...
 
E

Eric Sosman

Let's say you have a Vector of String objects. What is the easiest
way to remove duplicates?

The easiest way is to call the Vector's clear() method, which will
remove all duplicates. (It will also remove everything else, but if
the criterion is "easiest" this is surely the winner.)

If by "remove duplicates" you mean "retain one and only one
instance of each unique String," you can use a Set:

Vector<String> oldVec = ...;
Vector<String> newVec = new Vector<String>(
new HashSet<String>(oldvec));

Two things to note: First, this approach will do as advertised, but
will also scramble whatever order there may have been in oldVec.
Second, if there are five "X"'s in oldVec, there's no guarantee which
of them will get into newVec -- it could be any of the five.

If by "remove duplicates" you mean "retain only those Strings
that are unique, discarding all pairs, triples, et cetera," I know
of no pre-canned solution. You could sort the Vector and then sweep
over it looking for adjacent identical Strings. Or you could use a
pair of Sets and two passes, something like

Vector<String> vec = ...;
Set<String> seen = new HashSet<String>();
Set<String> dups = new HashSet<String>();
for (String s : vec) {
if (!seen.add(s)) {
dups.add(s); // second or subsequent sighting
}
}
for (Iterator<String> it = vec.iterator(); it.hasNext(); ) {
String s = it.next();
if (dups.contains(s)) {
it.remove();
}
}

Incidentally, Vector fell out of fashion several years ago.
Nowadays, the cognoscenti use List and its implementations.
 
R

Roedy Green

Let's say you have a Vector of String objects. What is the easiest
way to remove duplicates?

for just a few, scan backwards and do a delete. This will slide
objects down.

For many objects, copy just the objects you want to a new Vector
(ArrayList)
--
Roedy Green Canadian Mind Products
http://mindprod.com
The modern conservative is engaged in one of man's oldest exercises in moral philosophy; that is,
the search for a superior moral justification for selfishness.
~ John Kenneth Galbraith (born: 1908-10-15 died: 2006-04-29 at age: 97)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top