Loading...
Loading...
The attendance app saved the roll numbers of students who scanned the QR code. The list is sorted, but some students scanned more than once. Move one copy of each roll number to the front of the same array, without a second array. Then print how many different roll numbers there are, and the different roll numbers in order.
Example 1
Input
8 101 101 102 105 105 105 108 110
Output
5 101 102 105 108 110
101 and 105 were scanned more than once. Five different roll numbers remain.
Example 2
Input
3 7 7 7
Output
1 7
One student scanned three times.
Try the problem first. Open one hint at a time.
In a sorted list, all copies of the same roll number sit next to each other.
Use one index to read every value and a second index that marks where the next new value should be written.
A value is new when it differs from the last value you wrote.
When the read index finishes, the write index is the count k, and a[0] to a[k - 1] are the answer.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor