FastPrepReturn Distinct Elements
Problems/Return Distinct Elements

Return Distinct Elements

Alpaca logoAlpaca● EasyFULLTIMEPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: Multiple Alpaca interview summaries explicitly ask for distinct list elements. This practice version preserves first appearance, a documented ordering choice required for deterministic judging; the judged core matches about 95%.

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.

More Alpaca problems

  • Make a Sorted Array Unique In PlaceONSITE INTERVIEW · Seen Aug 2026
  • Hungarian Lottery Winner CountsOA · Seen Jun 2026
  • Aggregate a Large Dataset With GROUP BYPHONE SCREEN · Seen Sep 2022
See Alpaca hiring insights
Saved
class Solution {
    public int[] distinctElements(int[] values) {
      // Write your code here.
    }

}
Checking account…