np.unique() for unique values in NumPy - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time needed to find unique values in an array changes as the array gets bigger.
How does the work grow when we ask numpy to find unique items?
Analyze the time complexity of the following code snippet.
import numpy as np
arr = np.array([3, 1, 2, 3, 4, 1, 5])
unique_vals = np.unique(arr)
print(unique_vals)
This code finds all unique values in the array arr and returns them sorted.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Sorting the array elements to group duplicates.
- How many times: The sorting process compares elements multiple times, roughly proportional to the number of elements times the logarithm of that number.
As the array size grows, the time to find unique values grows a bit faster than the size itself but not as fast as the square of the size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 30 to 40 operations |
| 100 | About 600 to 700 operations |
| 1000 | About 10,000 to 12,000 operations |
Pattern observation: The operations grow faster than the input size but slower than its square, roughly like size times log of size.
Time Complexity: O(n log n)
This means the time to find unique values grows a bit faster than the number of items but not as fast as checking every pair.
[X] Wrong: "Finding unique values takes the same time no matter how many items there are."
[OK] Correct: The process needs to compare and sort items, so more items mean more work, not a fixed time.
Understanding how numpy finds unique values helps you explain how data processing scales, a useful skill when working with real datasets.
"What if the input array was already sorted? How would the time complexity change?"
Practice
np.unique() function do in NumPy?Solution
Step 1: Understand the purpose of
This function is designed to find all different (unique) values in a NumPy array or list.np.unique()Step 2: Compare with other options
Sorting, summing, or reversing are different operations and not whatnp.unique()does.Final Answer:
Finds all unique values in an array -> Option DQuick Check:
np.unique()= unique values [OK]
- Confusing unique with sorting
- Thinking it sums values
- Assuming it reverses array
arr?Solution
Step 1: Recall the correct function call
The functionuniqueis part of the NumPy module and is called asnp.unique().Step 2: Check other options for errors
arr.unique()is not a NumPy array method,unique(arr)misses the module prefix, andnp.arr.unique()is invalid syntax.Final Answer:
np.unique(arr) -> Option BQuick Check:
Correct syntax = np.unique(arr) [OK]
- Calling unique as method on array
- Missing np prefix
- Wrong module usage
import numpy as np arr = np.array([3, 1, 2, 3, 2, 1, 4]) print(np.unique(arr))
Solution
Step 1: Identify unique values in the array
The array contains values 3, 1, 2, 3, 2, 1, 4. Unique values are 1, 2, 3, and 4.Step 2: Understand
np.unique()output ordernp.unique()returns sorted unique values, so output is [1 2 3 4].Final Answer:
[1 2 3 4] -> Option AQuick Check:
Unique sorted values = [1 2 3 4] [OK]
- Ignoring sorting order
- Listing duplicates
- Wrong output format
import numpy as np arr = [1, 2, 2, 3] print(np.unique(arr, return_counts=True))
Solution
Step 1: Check input type compatibility
np.unique()accepts lists or arrays as input without error.Step 2: Verify
The argumentreturn_countsargumentreturn_counts=Trueis valid and returns counts of unique values.Final Answer:
No error, code runs fine -> Option CQuick Check:
List input + return_counts works [OK]
- Thinking list input causes error
- Wrong argument name
- Assuming return_counts unsupported
arr = np.array([[1, 2, 2], [3, 1, 4]])
How do you get all unique values from this 2D array as a sorted 1D array?
Solution
Step 1: Understand
Callingnp.unique()on 2D arraysnp.unique()without axis flattens the array and returns unique sorted values.Step 2: Check axis arguments
Usingaxis=0oraxis=1returns unique rows or columns, not unique elements overall.Final Answer:
np.unique(arr) -> Option AQuick Check:
Flatten and unique = np.unique(arr) [OK]
- Using axis to get unique elements
- Calling unique as method
- Expecting 2D unique output
