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


Groups > comp.lang.java.programmer > #29013 > unrolled thread

Finding Duplicate Values In An Array List

Started bygunitinug@gmail.com
First post2014-07-09 02:31 -0700
Last post2014-07-12 20:45 -0700
Articles 8 — 6 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  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

#29013 — Finding Duplicate Values In An Array List

Fromgunitinug@gmail.com
Date2014-07-09 02:31 -0700
SubjectFinding Duplicate Values In An Array List
Message-ID<92f9302e-854e-4162-ad22-87900b4e7d5a@googlegroups.com>
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<current_size; 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<current_size; 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!

[toc] | [next] | [standalone]


#29014

FromJoerg Meier <joergmmeier@arcor.de>
Date2014-07-09 11:35 +0200
Message-ID<fuhnm5fvett0.i0b4e0d0gwx7$.dlg@40tude.net>
In reply to#29013
On Wed, 9 Jul 2014 02:31:34 -0700 (PDT), gunitinug@gmail.com wrote:

> http://www.dreamincode.net/forums/topic/349987-finding-duplicate-values-in-an-array-list/

> My solution:

> [ Long list of code removed ]

My solution: Set<Integer> ints = new
HashSet<Integer>(Arrays.asList(intArray));

There, duplicates removed. Don't reinvent the wheel.

Liebe Gruesse,
		Joerg

-- 
Ich lese meine Emails nicht, replies to Email bleiben also leider
ungelesen.

[toc] | [prev] | [next] | [standalone]


#29015

Fromgunitinug@gmail.com
Date2014-07-09 02:40 -0700
Message-ID<4a2000a4-8c55-40d0-a9df-8593fdc88421@googlegroups.com>
In reply to#29014
On Wednesday, July 9, 2014 7:35:30 PM UTC+10, Joerg Meier wrote:
> On Wed, 9 Jul 2014 02:31:34 -0700 (PDT), gunitinug@gmail.com wrote:
> 
> 
> 
> > http://www.dreamincode.net/forums/topic/349987-finding-duplicate-values-in-an-array-list/
> 
> 
> 
> > My solution:
> 
> 
> 
> > [ Long list of code removed ]
> 
> 
> 
> My solution: Set<Integer> ints = new
> 
> HashSet<Integer>(Arrays.asList(intArray));
> 
> 
> 
> There, duplicates removed. Don't reinvent the wheel.
> 
> 
> 
> Liebe Gruesse,
> 
> 		Joerg
> 
> 
> 
> -- 
> 
> Ich lese meine Emails nicht, replies to Email bleiben also leider
> 
> ungelesen.

i was answering the guy's question.

[toc] | [prev] | [next] | [standalone]


#29016

FromJukka Lahtinen <jtfjdehf@hotmail.com.invalid>
Date2014-07-09 12:48 +0300
Message-ID<lv1ttuyiac.fsf@saunalahti.fi>
In reply to#29015
gunitinug@gmail.com writes:

> i was answering the guy's question.

What question? You didn't followup to anything!

-- 
Jukka Lahtinen

[toc] | [prev] | [next] | [standalone]


#29019

FromDaniele Futtorovic <da.futt.news@laposte.net>
Date2014-07-09 16:47 +0200
Message-ID<lpjko2$dbh$1@dont-email.me>
In reply to#29013
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 );
    }
  }

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<current_size; 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<current_size; 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!
> 

[toc] | [prev] | [next] | [standalone]


#29032

FromRobert Klemme <shortcutter@googlemail.com>
Date2014-07-10 19:24 +0200
Message-ID<c280hpF266aU1@mid.individual.net>
In reply to#29019
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.

Kind regards

	robert

[toc] | [prev] | [next] | [standalone]


#29071

FromDaniele Futtorovic <da.futt.news@laposte.net>
Date2014-07-12 16:18 +0200
Message-ID<lprg46$989$1@dont-email.me>
In reply to#29032
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.

[toc] | [prev] | [next] | [standalone]


#29085

FromRoedy Green <see_website@mindprod.com.invalid>
Date2014-07-12 20:45 -0700
Message-ID<n204s9p1tg1sadqqkrn3pel3l2qo8is1gf@4ax.com>
In reply to#29013
On Wed, 9 Jul 2014 02:31:34 -0700 (PDT), gunitinug@gmail.com wrote,
quoted or indirectly quoted someone who said :

>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<current_size; 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<current_size; 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!

There are two basic techniques

1. sort and compare pairwise

2. put element into a Set and notice when you have a dup.


Pretty well anything will work for small lists but simple minded
techniques have an nsquared problem with you scale them.
-- 
Roedy Green Canadian Mind Products http://mindprod.com
A program is only as good as its worst piece of code.
~ Joshua Cramer

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web