Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Write a function that returns `true` if two arrays have the same number of unique elements.
- #pYTHON pROGRAM
- #We can Use Hashing to solve this in O(n) time on average.
- #The idea is to traverse the given array from left to right
- #and keep track of visited elements in a hash set , as a set consists of only unique elements.
- # This function prints all distinct elements
- def countDistinct(arr, n):
- # Creates an empty hashset
- s = set()
- # Traverse the input array
- res = 0
- for i in range(n):
- # If not present, then put it in
- # hashtable and increment result
- if (arr[i] not in s):
- s.add(arr[i])
- res += 1
- return res
- #this is the function returns `true` and print `true` if two arrays have the same number of unique elements
- def CheckUniqeNoOfElement (arr1, arr2):
- n1 = len (arr1)
- n2 = len (arr2)
- if (countDistinct(arr1, n1) == countDistinct(arr2, n2)):
- print("true")
- else:
- print("false")
- arr1 =[2]
- arr2 =[3, 3, 3, 3]
- CheckUniqeNoOfElement (arr1, arr2)
Add Comment
Please, Sign In to add comment