DEV Community

Shakib S.
Shakib S.

Posted on

LeetCode: The “Contains Duplicate” Problem

Day 2 of Refusing to Write Code Without Understanding It.

There’s a specific question the computer is asking:

“Does this list have the same number appearing more than once?”
Simple. Almost boring.

But I made one rule for myself on this journey:

I don’t just want the green “Accepted” badge on LeetCode.

I want to see the code actually run.

I want to watch the output.

I want to feel the logic execute.

That rule has kept this process fulfilling.

The Problem (Explained)

Examples everyone understands:

[3, 7, 1, 9]   → false 
[3, 7, 3, 9]   → true 
[5]            → false 
[8, 8]         → true 
[]             → false
Enter fullscreen mode Exit fullscreen mode

We need a reliable way to detect if any number appears more than once.

Let’s solve it the most human way first.

The “Looking With Your Eyes” Method

Imagine this list:

[4, 1, 7, 2, 9, 4, 8]
Enter fullscreen mode Exit fullscreen mode

You would naturally do this:

Take 4 → remember it
Take 1 → new → remember
Take 7 → new
Take 2 → new
Take 9 → new
Take 4 → wait… I’ve seen 4 before → duplicate!
That’s the entire algorithm.
Enter fullscreen mode Exit fullscreen mode

Remember what you’ve seen.

If you see it again → stop.

Now let’s write that in code.

Turning Human Thinking Into Python

def has_duplicate(nums):
   seen = []                # empty list = our memory

   for num in nums:         # look at each number one by one
       if num in seen:      # is it already in memory?
           return True      # yes → duplicate found
       seen.append(num)     # no → remember it

   return False             # no duplicates
Enter fullscreen mode Exit fullscreen mode

Let’s mentally run it:

Input: [4, 1, 7, 4]
4 → add → [4]
1 → add → [4,1]
7 → add → [4,1,7]
4 → already in → return True

Perfect.

But here’s where things get interesting.

The Performance Reality

Lists in Python are slow at checking membership.

When you do:

if num in seen:
Enter fullscreen mode Exit fullscreen mode

Python checks every element in the list until it finds a match.

That’s O(n) time.

If the list has 100,000 elements, that check might scan through all 100,000.

And we do that inside a loop.

That makes the total time complexity:
O(n²)

For interviews? Not good.

Enter the Hash Map (via Set)

Python has a special data structure called a set. This is the secret sauce.

A set is implemented using a hash table (a structure that allows near constant-time lookup).

That means:

Checking num in seen becomes approximately O(1).

Now the entire loop becomes O(n).

Here’s the optimized version:

def has_duplicate(nums):
   seen = set()             # fast lookup structure

   for num in nums:
       if num in seen:
           return True
       seen.add(num)

   return False

Enter fullscreen mode Exit fullscreen mode

Same logic.
Different container.
Massive performance difference.
That’s the power of data structures.

The Lab Setup (The Rule I Follow. So Should You.)

This is where my personal rule kicks in.

I don’t just submit to LeetCode.

I build a tiny lab and run it myself.

class Solution:
   def containsDuplicate(self, nums):
       seen = set()
       for num in nums:
           if num in seen:
               return True
           seen.add(num)
       return False

s = Solution()
nums = [1, 0, 2, 5, 8, 9, 1]
result = s.containsDuplicate(nums)
print(result)
Enter fullscreen mode Exit fullscreen mode

Now I see:

True
Enter fullscreen mode Exit fullscreen mode

It’s more satisfying.

It feels real.

It feels engineered.

Not gamified.

What This Problem Actually Taught Me

This wasn’t about duplicates.

It was about:

  • Choosing the right data structure
  • Understanding time complexity
  • Thinking in terms of scale
  • Translating human logic into machine logic

LeetCode is helping me build algorithmic discipline.

And that discipline matters when:

  • Designing APIs
  • Optimizing backend systems
  • Handling large datasets
  • Preventing performance bottlenecks

Thanks for reading.

This week I’m focused on hash maps and strings.
Cheers to learning.

Top comments (0)