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


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

Finding Duplicate Values In An Array List

Newsgroups comp.lang.java.programmer
Date 2014-07-09 02:31 -0700
Message-ID <92f9302e-854e-4162-ad22-87900b4e7d5a@googlegroups.com> (permalink)
Subject Finding Duplicate Values In An Array List
From gunitinug@gmail.com

Show all headers | View raw


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!

Back to comp.lang.java.programmer | Previous | Next — 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