Use a single subscripted array to solve the following problem. Read in 20
numbers, each which is between 10 and 100, inclusive. As each number is read,
print it only if it is not a duplicate of a number already read. Provide for the
“worst” case in which all 20 numbers are different. Use the smallest possible way
to solve the problem.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[] numbers = new int[20];
int cur = 0;
for (int i = 0; i < 20; i++) {
int number = in.nextInt();
boolean unique = true;
for (int j = 0; j < cur; j++) {
if (numbers[j] == number) {
unique = false;
break;
}
}
if (unique) {
System.out.println(number);
numbers[cur++] = number;
}
}
}
}
Comments
Leave a comment