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


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

Re: Finding Duplicate Values In An Array List

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From Daniele Futtorovic <da.futt.news@laposte.net>
Newsgroups comp.lang.java.programmer
Subject Re: Finding Duplicate Values In An Array List
Date Sat, 12 Jul 2014 16:18:37 +0200
Organization A noiseless patient Spider
Lines 81
Message-ID <lprg46$989$1@dont-email.me> (permalink)
References <92f9302e-854e-4162-ad22-87900b4e7d5a@googlegroups.com> <lpjko2$dbh$1@dont-email.me> <c280hpF266aU1@mid.individual.net>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding 7bit
Injection-Date Sat, 12 Jul 2014 14:18:46 +0000 (UTC)
Injection-Info mx05.eternal-september.org; posting-host="277351eefca0988f38d5c27694c30508"; logging-data="9481"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+CZdqdbXHxDVs1yDns+VSa"
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0
In-Reply-To <c280hpF266aU1@mid.individual.net>
Cancel-Lock sha1:dumAla9SXNQPhkdFkjFXIC3L9YI=
Xref csiph.com comp.lang.java.programmer:29071

Show key headers only | View raw


On 2014-07-10 19:24, Robert Klemme allegedly wrote:
> On 09.07.2014 16:47, Daniele Futtorovic wrote:
>> Here's another, shorter one:
>>
>> public static void main(String[] ss){
>>    Scanner sc = new Scanner(System.in);
>>    Set<Integer> sieve = new HashSet<Integer>();
>>    List<Integer> stash = new ArrayList<Integer>(20);
>>
>>    while( stash.size() < 20 ){
>>      System.err.printf( "Enter %d more number(s) between 10 and 100:%n",
>> 20 - stash.size() );
>>      String raw = sc.next();
>>
>>      try {
>>        int input = Integer.parseInt(raw);
>>
>>        if( input < 10 || input > 100 ){
>>          System.err.println( "Out of range: " + input );
>>        }
>>        else if( ! sieve.add( input ) ){
>>          System.err.println( "Duplicate: " + input );
>>        }
>>        else {
>>          stash.add( input );
>>          System.err.println( stash );
>>        }
>>      }
>>      catch( NumberFormatException nfex  ){
>>        System.err.println( "Not a number: " + raw );
>>      }
>>    }
> 
> Similar amount of lines, but differently distributed
> 
> package golf;
> 
> import java.util.BitSet;
> import java.util.Scanner;
> 
> public class FiDuWa {
> 
>   private static final int LIMIT = 20;
>   private static final int MIN = 10;
>   private static final int MAX = 100;
> 
>   public static void main(String[] args) {
>     @SuppressWarnings("resource")
>     final Scanner scan = new Scanner(System.in);
>     final StringBuilder stash = new StringBuilder();
>     final BitSet used = new BitSet();
> 
>     while (used.cardinality() < LIMIT) {
>       final int i = Integer.parseInt(scan.next());
> 
>       if (i >= MIN && i <= MAX) {
>         if (!used.get(i)) {
>           if (!used.isEmpty())
>             stash.append(' ');
> 
>           stash.append(i);
>           used.set(i);
>           System.out.println(stash);
>         } else
>           System.err.println("Duplicate: " + i);
>       } else {
>         System.err.println("Not in range: " + i);
>       }
>     }
>   }
> }
> 
> The Java standard library is a remarkable beast and BitSets are probably
> underused.

It sure is and they may be. I reckon they do strongly appeal to
(C-family?) programmers used to bit manipulation. While I personally do
on occasion gladly indulge in the latter, it remains that I find they
lack a bit in heuristics. So while they are great tools, their place
would IMO be deep under the hood.

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


Thread

Finding Duplicate Values In An Array List gunitinug@gmail.com - 2014-07-09 02:31 -0700
  Re: Finding Duplicate Values In An Array List Joerg Meier <joergmmeier@arcor.de> - 2014-07-09 11:35 +0200
    Re: Finding Duplicate Values In An Array List gunitinug@gmail.com - 2014-07-09 02:40 -0700
      Re: Finding Duplicate Values In An Array List Jukka Lahtinen <jtfjdehf@hotmail.com.invalid> - 2014-07-09 12:48 +0300
  Re: Finding Duplicate Values In An Array List Daniele Futtorovic <da.futt.news@laposte.net> - 2014-07-09 16:47 +0200
    Re: Finding Duplicate Values In An Array List Robert Klemme <shortcutter@googlemail.com> - 2014-07-10 19:24 +0200
      Re: Finding Duplicate Values In An Array List Daniele Futtorovic <da.futt.news@laposte.net> - 2014-07-12 16:18 +0200
  Re: Finding Duplicate Values In An Array List Roedy Green <see_website@mindprod.com.invalid> - 2014-07-12 20:45 -0700

csiph-web