I have two Numpy arrays A and B, I want to find whether sorted(A) == sorted(B) or not? For ex: If A = [5,3,2,4] and B = [3,2,5,4], then it should evaluate to True.
Can this be achieved in linear time?
How about Counter from collections
>>> import collections
>>> a = collections.Counter([1,1,2,3])
>>> b = collections.Counter([3,1,2])
>>> a == b
False
>>> a = collections.Counter([1,1,2,3])
>>> b = collections.Counter([3,1,2,1])
>>> a == b
True
Construction happens in O(n) time for each list
edit: If anyone is unfamiliar with what counter does. It goes through the list and creates what is essentially a dictionary mapping of value to the number of occurrences that value.
So once we create these dictionaries of the form a = {'1': 2, '2': 1...} Which takes up to O(n) time we can compare the two dictionaries which also takes O(n) time.
In this case adding brackets works quite fast:
A = [5,3,2,4]
B = [3,2,5,4]
print((sorted(A) == sorted(B)))
Which yields:
True
A = [1,1,2,3]andB = [3,1,2], thenset(A)==set(B)would return TRUE but the answer is actually False. @Lamanussorted(A) == sorted(B)in your case? Can you tell us more about your constraints?