Lab126 Interview Question
Software Engineer / DevelopersCountry: United States
Keep a hashmap that keeps count of how many times we've seen an integer in an array. For simplicity assume no duplicates.
After a sweep through the N arrays, we have a hash map containing the number of occurrences for each integer. Return a list of integers that had an occurrence of N.
Finally fix the duplicate error. Every time you start scanning a new array you keep a hashset of values you've seen in that array so far. This way you know if you've hit a duplicate or not.
Time Complexity: O(N*M)
where N is the number of arrays and M is the size of the largest array given.
Space Complexity: O(N*M) if we have to store all of that into a hash map. O(K) actually where K is the size of the map we use.
solution for two array list:-
- sjain November 06, 2014- O(N^2) - brute force match between two array.
- O(nlogn) - sort both array in O(NlogN), then compare both the list, that will take O(N)
- O(N) - if array is not too big, then store contain of first array in hashmap, then iterate through the second array and check whether number is there in the hashmap or not.
for N integer array , take two array and use above solution, then create a new array with the intersection then use that with next array to find intersection.
repeat the steps till all array is covered.