Return Distinct Elements
Reported by candidates from Alpaca's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The whole Alpaca question comes down to one data structure: a hash set. The Return Distinct Elements problem was reported in August 2026, and it's a warm-up on paper. You get an integer array, you keep the first occurrence of each value, and you return them in original order. That's it. The risk isn't difficulty, it's overthinking or reaching for the wrong tool under a clock. If you blank, StealthCoder runs invisibly during the live OA and hands you the approach so a simple problem doesn't become a failed one.
The problem
You are given an integer array values. Remove duplicate occurrences so that each distinct value appears exactly once. Preserve the order of first appearance. Return the resulting array. Function distinctElements(values: int[]) → int[] Examples Example 1 values = [1,1,3,2,1,4,5,4] return = [1,3,2,4,5] Keep the first occurrences of 1, 3, 2, 4, and 5 in their original order. Example 2 values = [1,2,3] return = [1,2,3] Every value is already distinct. Example 3 values = [-1,-1,0,-1] return = [-1,0] Keep the first -1 and the first 0. Constraints 1 <= values.length <= 10^5. -10^6 <= values[i] <= 10^6.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Walk the array once. Keep a hash set of values you've already seen. If the current value isn't in the set, add it to the set and append it to the result. If it is, skip it. That's O(n) time and O(n) space, and it preserves first-appearance order because you append in traversal order. The common pitfall is sorting first. Sorting breaks order and costs O(n log n). Another trap is using a list for membership checks, which turns it into O(n^2) and risks timing out at 10^5 elements. Don't use a set alone as the output either, since set ordering depends on the language. In Python, dict.fromkeys works, but a set plus a result list is clearer. Negative values and the 10^6 range are no issue for hashing. If the pattern slips away mid-assessment, StealthCoder is the safety net that surfaces it fast.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Return Distinct Elements cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Alpaca's OA.
Alpaca reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Return Distinct Elements FAQ
How hard is the Alpaca Return Distinct Elements question really?+
It's easy. One pass with a hash set does it. The only way to miss is overcomplicating it with sorting or nested loops. If you know set membership is O(1) on average, you're basically done in a few lines.
What's the trick to keeping the original order?+
Append to the result the first time you see a value, in the same loop you scan with. The set only answers 'seen before?'. It never decides order. Because you traverse left to right, first-appearance order falls out for free.
Why not just sort and remove adjacent duplicates?+
Sorting destroys the original order, and the problem requires first-appearance order. You could sort indices, but that's extra work for no gain. The hash set approach is simpler, faster at O(n), and matches the requirement directly.
Will a nested loop pass with 10^5 elements?+
Probably not. Checking membership by scanning the result list makes it O(n^2), which is around 10^10 operations in the worst case. Use a hash set so each lookup is constant time on average.
How do I prepare for this in 48 hours?+
Write the set-plus-list solution from memory in your language. Test it on the three examples, including the negative number case [-1,-1,0,-1]. Also be ready for follow-ups like dedupe in place or keep the last occurrence instead. That covers the likely variations.