hi
i was writte program that implement one-d array
user enter 10 element without any duplication
it is give me all element its already exist

import java.util.Scanner;
public class Duplicates {
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        int[] array = new int[10];
        int[] copyarray = new int[10];
        System.out.println(" Enter 10 integers: ");
        int i ;
        for(i=0;i<array.length;i++)
        {
            array[i]= input.nextInt();
            copyarray[i] = array[i];
             if ( array[ i ] == copyarray[ i ] )
                       System.out.println("plz enter other element because uoyr element is already exist");
                       else
            System.out.println("\n  "+array[i]);




        }
}

what do you think this is doing?

array[i]= input.nextInt();
copyarray[i] = array[i];
if ( array[ i ] == copyarray[ i ] )
System.out.println("plz enter other element because uoyr element is already exist");
else
System.out.println("\n "+array[i]);

you set both values to the same value, so they are equal in value, then you check them for equality and show the error message if they are the same (which they always will be)

I think you've made some errors in your logic. you only need one array, not two

1. read number
2. check array if number already present
2.a if yes: error message
2.b if no: add the number to the array
import java.util.Scanner;
public class Duplicates {
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        int[] array = new int[10];
        System.out.println(" Enter 10 integers: ");
        int i ;
        for(i=0;i<array.length;i++)
        {
            array[i]= input.nextInt();
             if ( array[ i+1 ] == array[ i ] )
                       System.out.println("plz enter other element because uoyr element is already exist");
                       else
            System.out.println("\n  "+array[i]);




        }
}}

i make two array - the second as copy array of first
> i want it stop if a number present and alter user to enter other number ...
. i try to make what are you said :(
but i am still have porrly understand

unless you enter 0 as a value, this
array[ i+1 ] == array[ i ]
will never be true.

try writing a method that takes three parameters:
an array of integers, the current index and the value you're testing on.
check if (between element 0 and element on index 'currentIndex') one of your values equals the third parameter.
if so, return true, if not, return false.

this you can write in a separate method, that only needs about three or four lines of code and test seperately, and, it would solve .. well, basically your entire problem. just try that and when you've got it, use that method in your code.

Why did u utilize the 2nd Array?
To solve this,it's quite simple, in your for loop, when you enter the value no-i, you just check if that value equals to any value from 0 to (i-1) or not!
The algorithm here is:
for(i = 0 to arr.length) do
{
while(value_entered exist){
enter again;
}
arr = value_entered;
}
----
Songokute

import java.util.Scanner;

public class Duplicate {

    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        int[] array = new int[10];
        System.out.println(" Enter 10 integers: ");
        int i ;
        for(i=0;i<array.length;i++)
        {do{
        	while(array[ i ] != array[ i+1 ]);
        		array[i]= input.nextInt();
        }

        }
}

}

i didn't match your algorithm

Do you have an algorithm you are trying to use?
Can you write a list of pseudo code statements to describe it?

@Programing:
On line 13, you can not use (array != array[i+1]).

public class Duplicate {
 
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        int[] array = new int[10];
        System.out.println(" Enter 10 integers: ");
        int i ;
        for(i=0;i<array.length;i++)
        {do{
        	while(true){
                     System.out.println("Enter i-th number: ");
                     array[i] = input.nextInt();
                     for(int tmp = 0; tmp < i; i ++)
                        if(array[i] == array[tmp])
                           {
                                System.out.println("Duplicate number! Plz enter again!");
                           }
                         else break;
                }
        }
 
        }
}
 
}

---
Songokute

Oh im sorry, in my code, there is one mistake
It should be "for(int tmp = 0; tmp < i; tmp ++)"
not "for(int tmp = 0; tmp < i; i ++)"

just write an extra method that determines whether or not your input is already in the array.

public static boolean elementExists(int[] array, int input){
  for ( int test: array )
    if ( test == input )
      return true;
  return false;
}

now, just implement this in the code you already had.
an interesting lesson for you would be to
1. explain what above code does, line by line and (most importantly)
2. figure out which inputted value will NOT give the result you want, and why.

This article has been dead for over six months. Start a new discussion instead.