0% completed
Space and Time Complexity
Rohi Anon
Apr 30, 2023
I have another thought of how to proceed with this problem, but the problem is, I do not know the space and time complexity of this.
My thought process is as follows. Add all the elements in the array into a hash set. Count the number of existing elements in the two collections and compare to see whether there are any duplicates. The list collection should be equal to the set collection if there are no duplicates and return true.
I.e.
def containsDuplicate(nums):
`from collections import Counter`
`return Counter(nums) == Counter(set(nums))`
0
0
Comments
Jimmy 2 years ago
The time and space complexity will be O(N), where N is the size of the array. You will need to iterate through all elements of the array when creating the set and your set will have at most N items, if all elements are distinct.
On this page
Problem Statement
Examples
Solution
Approach 1: Brute Force
Approach 2: Using Hash Set
Approach 3: Sorting