Path: csiph.com!usenet.pasdenom.info!news.albasani.net!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Daniele Futtorovic Newsgroups: comp.lang.java.programmer Subject: Re: Finding Duplicate Values In An Array List Date: Wed, 09 Jul 2014 16:47:56 +0200 Organization: A noiseless patient Spider Lines: 124 Message-ID: References: <92f9302e-854e-4162-ad22-87900b4e7d5a@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Injection-Date: Wed, 9 Jul 2014 14:48:34 +0000 (UTC) Injection-Info: mx05.eternal-september.org; posting-host="322ed0cba703ac7b9691d01fc8a65214"; logging-data="13681"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/0B9CKZqd8LmUDWJ6/IeT2" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 In-Reply-To: <92f9302e-854e-4162-ad22-87900b4e7d5a@googlegroups.com> Cancel-Lock: sha1:0yqIsjYVFkJB9dumDIMugLskJj4= Xref: csiph.com comp.lang.java.programmer:29019 Here's another, shorter one: public static void main(String[] ss){ Scanner sc = new Scanner(System.in); Set sieve = new HashSet(); List stash = new ArrayList(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 ); } } On 2014-07-09 11:31, gunitinug@gmail.com allegedly wrote: > http://www.dreamincode.net/forums/topic/349987-finding-duplicate-values-in-an-array-list/ > > My solution: > > CLASS Populate: > > import java.util.*; > > class Populate { > > // goal: populate twenty entries for array. > // 1. get an input from keyboard an integer value. > // 2. check its between 10 and 100. > // 3. check its a duplicate. > // 4. add to the array. > // 5. print content of array after each insertion. > > static Scanner kb=new Scanner(System.in); > > int[] array=new int[20]; > int current_size; > int candidate; > > Populate() { > current_size=0; > } > > void generateCandidate() { > candidate=kb.nextInt(); > } > > void killKB() { > kb.close(); > } > > int getCandidate() { > return candidate; > } > > boolean checkBoundary(int chk) { > if (chk>=10 && chk<=100) return true; > else return false; > } > > // check an integer against all values of the array > boolean checkIsDuplicate(int chk) { > for (int i=0; i if (chk==array[i]) return true; > } > return false; > } > > void addEntry(int chk) { > array[current_size]=chk; > current_size++; > } > > void print() { > for (int i=0; i System.out.print(array[i]); > System.out.print(" "); > } > System.out.println(); > } > } > > CLASS PopulateMain > > class PopulateMain { > public static void main(String[] args) { > Populate pop=new Populate(); > > // how to populate twenty entries? > int total=0; > while (total<20) { > pop.generateCandidate(); > int c=pop.getCandidate(); > if (pop.checkBoundary(c) && !pop.checkIsDuplicate(c)) { > pop.addEntry(c); > total++; > > // print only after adding an entry. > pop.print(); > } > } > //pop.killKB(); > } > } > > God BLESS YOU! >