I've checked everything but still duplicate the last array for some reason. What seems to be the problem? In the Program, the first thing is to ask a user to input array size, then input numbers to that size. Next, the code shall remove the duplicate number that a user inputted. Lastly, the output will display the elements of the array without any duplicate.
Below is the program/code for the same:-
import java.util.Scanner;
public class Finals
{
private static Scanner sc;
public static void main(String[] args)
{
int tao, hayop, counter, bilang = 1, result, taonghayop;
sc = new Scanner(System.in);
System.out.print("Enter array size: ");
int userInput = sc.nextInt();
int userInput1 = userInput;
int[] userDatabase = new int[userInput];
for(counter=0;counter<userInput;++counter)
{
System.out.print("Enter array elements of index " +bilang +": ");
userDatabase[counter] = sc.nextInt();
bilang++;
}
for(tao=0;tao<userInput;++tao)
{
for(hayop=tao+1;hayop<userInput;)
{
if(userDatabase[tao] == userDatabase[hayop])
{
for(taonghayop = hayop; taonghayop<userInput;taonghayop++)
{
userDatabase[taonghayop] = userDatabase[taonghayop+1];
}
userInput = userInput-1;
}
else
hayop++;
}
}
System.out.print("The number(s) are: " + userDatabase[counter]);
for (result=0; result<=userInput1; result++)
{
if (result<userInput1)
{
System.out.print(" ");
System.out.print(userDatabase[result]);
System.out.print(",");
}
else if(result==userInput1)
{
System.out.print(" and ");
System.out.print(userDatabase[result]);
System.out.print(".");
break;
}
}
}
}