Order-Preserving Dedup of a NumPy Array

Quick Overview

Return the distinct values of a 1-D NumPy array in the order they first appear, without the sorting that np.unique applies. It tests how to recover original positions from np.unique, vectorized indexing, complexity, and edge cases like empty input and NaN.

Order-Preserving Dedup of a NumPy Array

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Given a 1-D array, return a 1-D array of its distinct values ordered by first appearance (unlike `np.unique`, do not sort). Requirements: - Each distinct value appears exactly once, in first-occurrence order. - A vectorized approach is preferred, for example `np.unique(arr, return_index=True)` and then sorting by the returned indices. - Empty input returns an empty array. Example: ```python arr = np.array([3, 1, 3, 2, 1, 5]) dedup(arr) # -> array([3, 1, 2, 5]) ``` ```hint Know which array an index points into Before you index with anything `np.unique` returns, be clear whether those positions refer to the original array or to the sorted unique values. ``` ### Constraints & Assumptions - The input is a 1-D NumPy array and may be empty. ### What a Strong Answer Covers - Why `np.unique` on its own loses the original order, and which extra output recovers it. - How the first-occurrence indices turn back into values in appearance order, with the indexing done on the right array. - Time and space cost, and where the sort comes from. - Empty input and the dtype of the result. ### Follow-up Questions - How does `return_index=True` let you recover first-appearance order? - How would you extend this to dedup rows of a 2-D array? - How would you do this in linear time on a very large array, and what would you give up?

Overview: Return the distinct values of a 1-D NumPy array in the order they first appear, without the sorting that np.unique applies. It tests how to recover original positions from np.unique, vectorized indexing, complexity, and edge cases like empty input and NaN.

General logo
General
Nov 24, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

Given a 1-D array, return a 1-D array of its distinct values ordered by first appearance (unlike np.unique, do not sort).

Requirements:

  • Each distinct value appears exactly once, in first-occurrence order.
  • A vectorized approach is preferred, for example np.unique(arr, return_index=True) and then sorting by the returned indices.
  • Empty input returns an empty array.

Example:

arr = np.array([3, 1, 3, 2, 1, 5])
dedup(arr)  # -> array([3, 1, 2, 5])

Constraints & Assumptions

  • The input is a 1-D NumPy array and may be empty.

What a Strong Answer Covers Guidance

  • Why np.unique on its own loses the original order, and which extra output recovers it.
  • How the first-occurrence indices turn back into values in appearance order, with the indexing done on the right array.
  • Time and space cost, and where the sort comes from.
  • Empty input and the dtype of the result.

Follow-up Questions Guidance

  • How does return_index=True let you recover first-appearance order?
  • How would you extend this to dedup rows of a 2-D array?
  • How would you do this in linear time on a very large array, and what would you give up?