Q110FreeSystemVerilog
Remove duplicates while preserving order
Interview prompt
Question
Remove duplicate integers from a sequence while preserving the first occurrence of every value in its original order.

Candidate starting point
Implementation scaffold
function automatic void remove_duplicates(
ref int in[$],
ref int out[$]
);
bit seen[int];
int snapshot[$];
// Implement here: keep first occurrences in encounter order.
endfunctionReviewed example
Trace one case
Input
values=[3,1,3,2,1,4]Expected output
[3,1,2,4]Only the first occurrence of each integer is emitted, so the original encounter order is preserved.
What to cover
Requirements
- Clear any previous output.
- Emit a value only on its first appearance.
- Preserve first-occurrence ordering.
- The same queue variable may be passed as input and output; snapshot its original values before clearing output.
