217. Contains Duplicate

217. Contains Duplicate

Code

class Solution
    def containsDuplicate(self, nums: List[int]) -> bool:
        hash_set = set()
        for item in nums:
            if item in hash_set:
                return True
            hash_set.add(item)
        return False:        

Time Complexity: O(n)

Space Complexity: O(n)

Iterating through each element in the input array "nums" and testing in O(1) time if we've encountered each value "x" previously. When we discover a duplicate, we return "True". We return "False" if the loop concludes without discovering any duplicates.

A hash table is used to implement the set data type in Python. On average, the hash table provides for efficient lookups and insertion of entries in constant time, implying that the time complexity of most set operations is O(1). When there are hash collisions, however, the time complexity can be O(n), where n is the number of members in the set.

要查看或添加评论,请登录

Irfan Ahmed Mohammad的更多文章

  • Exploring Creational Design Patterns in Real-Life Applications: From Singleton to Prototype

    Exploring Creational Design Patterns in Real-Life Applications: From Singleton to Prototype

    Creational design patterns in Java provide ways to create objects while abstracting the instantiation process. This…

  • Exploring Structural Design Patterns in Java: From Adapter to Flyweight

    Exploring Structural Design Patterns in Java: From Adapter to Flyweight

    Introduction: In the realm of software design, Structural Design Patterns play a crucial role in organizing code that…

  • System Design Essentials: The Role of API Gateway

    System Design Essentials: The Role of API Gateway

    In the realm of system design interviews, understanding the pivotal role of API Gateways (AGs) is not just advantageous…

    2 条评论
  • System Design Essentials: The Role of Load Balancers

    System Design Essentials: The Role of Load Balancers

    In system design interviews, think of Load Balancers (LB) as gatekeepers at the entrance of a network. They're like…

    1 条评论
  • 268. Missing Number

    268. Missing Number

    Code: class Solution: def missingNumber(self, nums: List[int]) -> int: n = len(nums) res = n…

  • 190. Reverse Bits

    190. Reverse Bits

    Code class Solution: def reverseBits(self, n: int) - int: res = 0 for i in range(32):…

  • 191. Number of 1 Bits

    191. Number of 1 Bits

    Code class Solution: def hammingWeight(self, n: int) -> int: while n: n &= (n-1)…

  • 128. Longest Consecutive Sequence

    128. Longest Consecutive Sequence

    Code class Solution: def longestConsecutive(self, nums: List[int]) -> int: seen = set(nums) res = 0…

  • 238. Product of Array Except Self

    238. Product of Array Except Self

    Code class Solution: def productExceptSelf(self, nums: List[int]) -> List[int]: res = [1]*len(nums)…

  • 49. Group Anagrams

    49. Group Anagrams

    Approach 1 Using Sorted string as key class Solution def groupAnagrams(self, strs: List[str]) -> List[List[str]]:…

    1 条评论

社区洞察

其他会员也浏览了