![]() |
VOOZH | about |
Sometimes, while working with data, we can have a problem in which we need to check the occurrences of records that occur at similar times. This has applications in the web development domain. Let's discuss certain ways in which this task can be performed.
Method #1 : Using map() + Counter() + sorted
The combination of the above functionalities can be used to perform this task. In this, before feeding data to Counter(), for counting occurrences, sort the data to make all unordered tuple ordered to count similar ones as one.
The original list is : [(3, 1), (1, 3), (2, 5), (5, 2), (6, 3)]
The frequency of like tuples : {(2, 5): 2, (1, 3): 2, (3, 6): 1}Time complexity: O(nlogn), where n is the length of the test_list. The map() + Counter() + sorted takes O(nlogn) time
Auxiliary Space: O(n), extra space of size n is required
Method #2: Using frozenset() + Counter() The combination of above functions can be used to perform this particular task. In this, the task performed by sorted and map() is performed by the frozenset() which orders the tuples internally.
The original list is : [(3, 1), (1, 3), (2, 5), (5, 2), (6, 3)]
The frequency of like tuples : {(2, 5): 2, (1, 3): 2, (3, 6): 1}Time Complexity: O(n), where n is the length of the list test_dict
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list
Method #3: Using sort() and count() methods
The original list is : [(3, 1), (1, 3), (2, 5), (5, 2), (6, 3)]
The frequency of like tuples : {(1, 3): 2, (2, 5): 2, (3, 6): 1}Method #4: Using defaultdict
Use defaultdict container from the collections module to group similar tuples and their occurrences.
Step-by-step approach:
Below is the implementation of the above approach:
The original list is : [(3, 1), (1, 3), (2, 5), (5, 2), (6, 3)]
The frequency of like tuples : {(1, 3): 2, (2, 5): 2, (3, 6): 1}Time complexity: O(nlogn), where n is the number of tuples in the given list. Sorting the tuples takes O(nlogn) time, and incrementing the count in the defaultdict takes O(1) time.
Auxiliary space: O(n), where n is the number of tuples in the given list. The defaultdict and the sorted tuples are stored in memory.
Method #5: Using set() and count()
The original list is : [(3, 1), (1, 3), (2, 5), (5, 2), (6, 3)]
The frequency of like tuples : {frozenset({1, 3}): 2, frozenset({2, 5}): 2, frozenset({3, 6}): 1}Time complexity: O(n) where n is the length of test_list. This is because we loop through test_list only once.
Auxiliary space: O(n) to store freq_dict.
Method #6: Using Dictionary
The original list is : [(3, 1), (1, 3), (2, 5), (5, 2), (6, 3)]
The frequency of like tuples : {(1, 3): 2, (2, 5): 2, (3, 6): 1}Time complexity: O(nlogn) (sorting each tuple)
Auxiliary space: O(n) (creating a dictionary to store the counts)
Method #7: Using reduce():
The original list is : [(3, 1), (1, 3), (2, 5), (5, 2), (6, 3)]
The frequency of like tuples : {(1, 3): 2, (2, 5): 2, (3, 6): 1}Time Complexity:
Sorting the tuples takes O(k log k) time, where k is the length of the tuple.
Since the lambda function is applied to each tuple in the list, the time complexity of the reduce() method is O(n), where n is the length of the list.
Therefore, the total time complexity of this algorithm is O(n k log k).
Space Complexity:
We use a defaultdict to store the frequency of similar tuples, so the space complexity of this algorithm is O(m), where m is the number of distinct sorted tuples in the list. In the worst case scenario, where all tuples are distinct, the space complexity is O(n).
Method #8 : Using sort() and operator.countOf() methods
The original list is : [(3, 1), (1, 3), (2, 5), (5, 2), (6, 3)]
The frequency of like tuples : {(1, 3): 2, (2, 5): 2, (3, 6): 1}Time Complexity: O(N log N) N - length of tuples list
Auxiliary Space : O(N) N - length of dictionary