0
0
Data Analysis Pythondata~5 mins

nunique() for cardinality in Data Analysis Python - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: nunique() for cardinality
O(n)
Understanding Time Complexity

We want to understand how the time to count unique values grows as the data size increases.

How does the work change when we have more rows to check for unique items?

Scenario Under Consideration

Analyze the time complexity of the following code snippet.

import pandas as pd

data = pd.Series([1, 2, 2, 3, 4, 4, 4, 5])
unique_count = data.nunique()
print(unique_count)

This code counts how many different values are in the data series.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: Checking each element to see if it is unique.
  • How many times: Once for each item in the data series.
How Execution Grows With Input

As the number of items grows, the work grows roughly in the same way.

Input Size (n)Approx. Operations
10About 10 checks
100About 100 checks
1000About 1000 checks

Pattern observation: The number of operations grows directly with the number of items.

Final Time Complexity

Time Complexity: O(n)

This means the time to count unique values grows linearly with the number of items.

Common Mistake

[X] Wrong: "Counting unique values is instant no matter how big the data is."

[OK] Correct: The function must look at each item at least once, so bigger data means more work.

Interview Connect

Understanding how counting unique items scales helps you explain data processing speed clearly and confidently.

Self-Check

"What if we used a sorted list before counting unique values? How would the time complexity change?"