I need to write a program for someone to enter 5 numbers from 10 to 100 but I don't know how i can eliminate duplicates? Anybody got any bright ideas on how i can do it?
            Asked
            
        
        
            Active
            
        
            Viewed 1,642 times
        
    -1
            
            
        - 
                    possible duplicate of [How do I remove repeated elements from ArrayList?](http://stackoverflow.com/questions/203984/how-do-i-remove-repeated-elements-from-arraylist) – user541686 Feb 23 '11 at 23:29
- 
                    Do you have any idea how to do some of it? Also, if this is homework, you should tag it as such. – unholysampler Feb 23 '11 at 23:29
- 
                    Is this homework/college work - please tag it as such. Search this site first ? What ideas have you had so far ? Which specific question are you asking ? How to handle I/O ? How to store 5 numbers ... etc.. }-| – David Victor Feb 23 '11 at 23:33
- 
                    Struth. Talk about painting the Forth Bridge. Add the numbers to a Set (if that is the question) & there will be no duplicates. – David Victor Feb 24 '11 at 22:13
5 Answers
2
            
            
        Add them into hashset, and check how many elements you have in set.
For example:
    Scanner sc = new Scanner(System.in);
    HashSet<Integer> nrs = new HashSet<Integer>();
    int n;
    System.out.println("Enter 5 numbers from 10 to 100");
    do {
        if (sc.hasNextInt())
            if ((n = sc.nextInt()) > 9 && n < 101)
                nrs.add(new Integer(n));
        System.out.println("We have numbers: " + nrs);
    } while (nrs.size() < 5);
    System.out.println("Superb. You entered numbers: " +nrs);
 
    
    
        Margus
        
- 19,694
- 14
- 55
- 103
1
            Use a Set implementation whic by definition does not contain duplicates...
TreeSet<Integer> ints = new TreeSet<Integer>();
int[] int_array = { 1, 2, 3, 4, 3, 5, 4, 2, 3, 4, 3 4, 2, 1 };
for (int n : int_arr)
   ints.add(n);
for (int n : ints)
  System.out.preintln(n);
outputs: 1 2 3 4 5
 
    
    
        Aaron Gage
        
- 2,373
- 1
- 16
- 15
- 
                    You should declare via the interface i.e. Setints ..., lack of braces on the for statements also is not the best example. – David Victor Feb 24 '11 at 22:11
0
            
            
        Keep track of numbers entered so far. If a user's input has been entered already ask the user to re-enter another number.
 
    
    
        Asterisk
        
- 3,534
- 2
- 34
- 53
0
            
            
        This is not as trivial as it seems.
But after a little while I came to this:
class ClearDup { 
    public static void main( String ... args ) { 
        int [] o = {1,2,1,3,1,2,4};
        int [] f = new int[o.length];
        int filterIndex = 0;
        original: for( int i : o ) { 
            for( int j : f ) { 
                if( i == j ) { 
                    continue original;
                }
            }
            f[filterIndex++] = i;
        }
        int [] result = java.util.Arrays.copyOf(f, filterIndex );
        System.out.println( java.util.Arrays.toString( result ) );
    }
}
 
    
    
        OscarRyz
        
- 196,001
- 113
- 385
- 569
- 
                    instead of your array-creation + arraycopy you could call `int[] result = Arrays.copyOf(f, filterIndex);` – Paŭlo Ebermann Feb 24 '11 at 00:24
- 
                    I was trying not to relay too much on Arrays class but in this case makes sense :) Updated! thanks – OscarRyz Feb 24 '11 at 00:29
0
            
            
        class Aaaray{
    static int arr[]={1,2,3,4,2,3,4,3,5,6,4,4,1,6,7,3,8,7,9,10,3,10,11,11,12,1,2,5,13,14,12,14};
    static int brr[]= new int[8];
    public static void main(String[]args){
        for(int j=0;j<arr.length;j++){
            for(int i=j+1;i<arr.length;i++){
                if(arr[j] == arr[i]){
                    arr[i]=0;
                }
            }
        }
        for(int m=0;m<arr.length;m++){
            if(arr[m]!=0){
                int t =arr[m];
                System.out.println(t);
            }
        }
    }
} 
- 
                    does that imply that 0 is no possible value? That is my be a proper solution for exactly this question, but isn't there a better solution? – Angelo.Hannes Nov 15 '12 at 09:08
 
     
    