hi every body i have a problem whit the condition to check if the element is duplicate element or not
this is the problem

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

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]);
                    
                }
            
                
            }
            
        }
}

Recommended Answers

All 4 Replies

Actually you should have something like this:

int index = 0;
for (int i=0;i<array.length;i++) {
  int num;
  // read int number
  System.out.[B]print[/B](num+" ");

  // loop again through the array to see if that number already exists
  if (doesn't exists) {
      // put it into the array
     [B]array[index] = num;
     index++;[/B]
  } 
}
System.out.[B]println[/B]();

The for loop above will not loop the array. Is a way to count the numbers given. For putting things into the array you will need another 'index' which you will only increase every time a new value is actually entered into the array.

The index value tells you how many value you have entered into the array:

[B]array[index] = num;
     index++;[/B]

At the beginning index has value 0. When you enter something it becomes 1. So you have 1 element and the next would be put into the '1' position.
Whenever you loop the array use the index to tell you how many elements you have.

After the above loop print the elements of the array.

>>note the question ask to reprint the array but without any repeating number

You can use the library sort method and then display the first element.
Compare it with its adjacent, if different display it, and repeat.

how i can use the library sort method

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.