THINK FIRST·CODE LATER

← All labs

Distinct numbers

Problem

Distinct reads n, then n whole numbers, and prints how many different values there are, the different values in the order of their first appearance, and how many repeated values were skipped.

Store the distinct values in an array of size n together with a counter size of the places in use. Write a method indexOf(int[] list, int size, int value) that searches the first size elements. For the input

10
1 2 3 2 1 6 3 4 5 2

the program prints

Numbers read: 10
Distinct numbers: 6
In order of first appearance: 1 2 3 6 4 5
Repeated values skipped: 4

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

l9-7-data-1.txt

10
1 2 3 2 1 6 3 4 5 2
What your program must print for l9-7-data-1.txt
Numbers read: 10
Distinct numbers: 6
In order of first appearance: 1 2 3 6 4 5
Repeated values skipped: 4

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < l9-7-data-1.txt

Windows PowerShell does not understand <:

Get-Content l9-7-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.