Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #7572

Re: remove duplicates?

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail
From Stanimir Stamenkov <s7an10@netscape.net>
Newsgroups comp.lang.java.programmer
Subject Re: remove duplicates?
Date Mon, 05 Sep 2011 12:36:17 +0300
Organization A noiseless patient Spider
Lines 28
Message-ID <j42569$n2g$1@dont-email.me> (permalink)
References <c299b494-241e-481f-b993-a6272f15b915@j1g2000yqn.googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
Injection-Date Mon, 5 Sep 2011 09:36:09 +0000 (UTC)
Injection-Info mx04.eternal-september.org; posting-host="R6EdBNp5N5NfsbZn1ifSpQ"; logging-data="23632"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19C4jkwI4lh9DvgY4v6rtIn"
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0.1) Gecko/20110830 SeaMonkey/2.3.1
In-Reply-To <c299b494-241e-481f-b993-a6272f15b915@j1g2000yqn.googlegroups.com>
X-Face )>>ChyF_H<b)u~Zjgo/=wa~;=qyW%.F\L.d^fKL[;y\=tY\]M}2t(a^;PKS}9g|k@\vkA<P Q|4?kcJ52334f:CaCrQZ=]D~txPPh6[y{xHkZ+4/KPKZ~|*K#?EqeP0W]iU*Ldy-hyjh0)N4c.I<m) K}GsUUe0)~24Xp`Jt
Cancel-Lock sha1:kEuOyx0bACCDdmtmDURCLbjJ2QQ=
Xref x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:7572

Show key headers only | View raw


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();
         ...

-- 
Stanimir

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

remove duplicates? bob <bob@coolgroups.com> - 2011-09-05 01:44 -0700
  Re: remove duplicates? RedGrittyBrick <RedGrittyBrick@spamweary.invalid> - 2011-09-05 10:23 +0100
  Re: remove duplicates? Stanimir Stamenkov <s7an10@netscape.net> - 2011-09-05 12:36 +0300
  Re: remove duplicates? rossum <rossum48@coldmail.com> - 2011-09-05 12:46 +0100
  Re: remove duplicates? Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-09-05 08:38 -0400
  Re: remove duplicates? Patricia Shanahan <pats@acm.org> - 2011-09-05 05:51 -0700
  Re: remove duplicates? Roedy Green <see_website@mindprod.com.invalid> - 2011-09-05 11:07 -0700

csiph-web