tags:

views:

521

answers:

9

hi every body i have problem with this question

Duplicate Elimination Use a one-dimensional array to solve the following problem: Write an application that inputs 10 integers. As each number is read, display it only if it is not a duplicate of a number already read. Use the smallest possible array to solve this problem. Display the complete set of unique values input after the user inputs all values.

Sample Output:

Enter 10 integers:

12 33 67 9 10 6 6 34 10 19

Unique Values:

12 33 67 9 10 6 34 19 note the question ask to reprint the array but without any repeating number

and this is my code

import java.util.Scanner;

public class duplicate 
{

public static void main(String[] args)
{
    Scanner input = new Scanner(System.in);
    int[] array = new int[10];
    int[] barray = new int[10];

    System.out.println(" Enter 10 integers: ");
    int i ;
    for(i=0;i<array.length;i++)
    {
        array[i]= input.nextInt();
        barray[i] = array[i];
    }
    for(i=0;i<array.length;i++)
    {
        System.out.printf("\t %d ",array[i]);

    }
    System.out.println("\n Unique values are: ");


        for ( i = 0; i < array.length; i++ )
        {


            {
                if ( array[ i ] == barray[ i ] )
                    break;
                  System.out.printf("\t %d",array[i]);

            }


        }

    }

}

+1  A: 

Assuming that your problem is that the "print duplicates" part does not work: can you use a Vector? If so, you could do something like this when printing the unique values:

for each item in the array
   if the vector does not contain the item
      print the value
      insert the item in the vector
Konamiman
Indeed. Anyway I guess that at this point the author should clarify its question a little.
Konamiman
If he could use Vector, then why not a Set? That's even easier. - On second thought, Set changes the sequence of the values, it's either undefined (HashSet) or ordered (TreeSet). So List<Integer> is better :)
Andreas_D
@Andreas_D you sir need to learn about LinkedHashSet
ILMTitan
Or, more generally, the SortedSet interface.
bcat
A: 

For your last loop, I'd consider something like this:

for(int i = 0; i < array.length; i++)
{
    Boolean duplicated = false;
    for(int j = 0; j<i; j++) {
       if(array[i] == array[j]) {
         //number already printed
         duplicated = true; 
         break;
       }
    }
    if(duplicated == false) //print number here.
}

It's not the best way to do it, but It follows what you already did.
The best way would be to use an adequate structure, I'm not a Java user but maybe HashSet is the way to go.

Soufiane Hassou
yes i must use arraythe problem is 1- print tthe orginal array 2- print the array with no repeating number
tootaa
Is the solution, I provided, working for you ? I'm using an array, the `HashSet` was just a suggestion anyway.
Soufiane Hassou
+1  A: 

Have you considered your problem without focusing on code? Imagine I gave you a bit of paper and asked you to write down all the numbers I shouted out, without duplicates, then read the numbers back to me when I was done. Think how you'd solve that in the real world before writing the code. E.g. would you write down numbers twice then score them out later? Would you need 2 sheets of paper - one with duplicates and one without?

monorailkitty
+4  A: 

Do you really need to use arrays? A Set would be ideal here:

public static void main(String[] args) {
 Scanner input = new Scanner(System.in);
 Set<Integer> set = new HashSet<Integer>(10);

 System.out.println(" Enter 10 integers: ");

 for (int i = 0; i < 10; i++) {
  set.add(input.nextInt());
 }
 System.out.println("Unique values are: ");

 for (Integer i : set) {
  System.out.printf("\t %d", i);
 }
}
bruno conde
Giving a complete solution does not help his learning.
rsp
The assignment requires the use of a one-dimensional array.
Kelly French
Using a LinkedHashSet will also preserve order.
Peter Lawrey
A: 

What do you think about:

Scanner input = new Scanner(System.in);
int[] array = new int[10];
System.out.println("Enter 10 integers:");
for (int i = 0; i < array.length; i++)
 array[i] = input.nextInt();
for (int i = 0; i < array.length; i++)
 System.out.print(array[i] + ", ");
System.out.println();
System.out.println("Unique values are: ");
for (int i = 0; i < array.length; i++) {
 boolean show = true;
 for (int j = 0; j < i; j++)
  if (array[j] == array[i]) {
   show = false;
   break;
  }
 if (show)
  System.out.print(array[i] + ", ");
}
mykhaylo
thank you its really workbut is their another way without using boolean OR Not
tootaa
If You can use only one-dimensional array - no possible to solve this problem without boolean values. You need to check whether the value has already been displayed. Set - is the best collection to solve this problem ;)
mykhaylo
A: 

The Arrays class http://java.sun.com/javase/6/docs/api/java/util/Arrays.html have some useful methods for this topic, like sort.

  1. Sort array
  2. copy first integer to a new output array
  3. Loop over all integers
  4. if previous integer differs from current
  5. create new array with output array size + 1 and copy all values finally add last value

Skipping input, the loop could look like:

    int[] out = new int[1];  
 int[] ints = new int[]{3,56,2,98,76,4,9,2,3,55};
 Arrays.sort(ints);
 out[0] = ints[0];          //handle first value
 for(int i = 1; i < ints.length; i++){   
  if(ints[i-1] != ints[i]){    
   int[] temp = new int[out.length+1];
   for(int j = 0; j < temp.length-1; j++){
    temp[j] = out[j];      //copy all previous
   }    
   temp[temp.length - 1] = ints[i];   //add last value
   out = temp;
  }
 }
 System.out.println(Arrays.toString(out));
Kennet
A: 

How about using a BitSet since all the values are integers.

BitSet bs = new BitSet();

for(int i=0; i<array.lenght; i++)
{
    bs.set(array[i]);    
}

System.out.println("Unique Values are: "+bs.toString());
coolest_head
+1  A: 

Two things to change:

  1. Use only a single array
  2. Filter duplicates on input, not output.

If you loop over your array for each input looking for dups. Dup? then don't insert, not dup? add to the array.

Using the smallest array possible is a trick because it depends on knowing how many dups will be input ahead of time which you may not. There are two solutions, use an array that is only big enough for the unique numbers in the assignment, or when you exceed the size of the array, create a new one and copy all the values over to it. Does the assignment mean you are limited to using one one instance of an array which happens to be single-dimensional or only that the array you use must be one-dimensional but you can use more than one array?

Kelly French
A: 

You can also try a shorter, if more obscure...

System.out.println("Unique values "+
    new LinkedHashSet<String>(Arrays.asList(scanner.nextLine().split(" +"))));
Peter Lawrey