Answer to Question #298917 in Java | JSP | JSF for rim

Question #298917

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.


1
Expert's answer
2022-02-17T11:52:42-0500
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;
            }
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS