#K15396. Remove Duplicates to Ensure Exactly Two Occurrences
Remove Duplicates to Ensure Exactly Two Occurrences
Remove Duplicates to Ensure Exactly Two Occurrences
Given a sorted array of integers, your task is to form a new array where every distinct integer that appears at least twice in the input appears exactly twice in the output. In other words, for each integer \(x\) in the input, if \(\text{count}(x) \ge 2\), then include exactly two copies of \(x\) in the output; otherwise, ignore \(x\). If no integer meets the requirement, output an empty array.
Examples:
- Input: [1, 1, 2, 2, 2, 3, 3] → Output: [1, 1, 2, 2, 3, 3]
- Input: [1, 2, 3, 4, 5] → Output: []
- Input: [1, 1, 1, 1, 1, 1] → Output: [1, 1]
Note: The input array is guaranteed to be sorted in non-decreasing order.
inputFormat
The first line of input contains an integer \(n\) representing the number of elements in the array. The second line contains \(n\) space-separated integers in non-decreasing order.
outputFormat
Output the resulting array in list format. Print the array as a comma-separated list enclosed within square brackets. If no integer appears at least twice, output an empty list []
.
7
1 1 2 2 2 3 3
[1, 1, 2, 2, 3, 3]