Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
A better concise solution

Aye Ess

May 2, 2023

class Solution: def containsDuplicate(self, nums): numsSet = set(nums) return not (len(nums) == len(numsSet))

5

0

Comments
Comments
B
boyueshen 2 years ago

I thought of the same solution. And I thought it was better since it's the same as Approach 2 but much neater. But I'm 100% sure.

Jay Karlsven
Jay Karlsven2 years ago

Yeah I've seen this problem before and this is, in my opinion, the best and most concise approach.

I'm still learning Big O though, so I'm wondering if I'm thinking about this correctly?

Time Complexity: O(1) because we are not iterating, just doing a single compar...

Papaya Pepperoni
Papaya Pepperoni2 years ago

Not as good as Approach 2 in some cases because there's no early return.

On this page

Problem Statement

Examples

Solution

Approach 1: Brute Force

Approach 2: Using Hash Set

Approach 3: Sorting