📌Map | 📌Simple & Neat Explanation | 📌 Clean C++, Java code
Intuition\n Describe your first thoughts on how to solve this problem. \nWe need to find the intersection of elements present in all the sub-arrays of the given
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe need to find the intersection of elements present in all the sub-arrays of the given 2D integer array nums. For this, we can create a hashmap where we store the frequency of each element present in any sub-array of nums. Then, we traverse the hashmap and if the frequency of any element is equal to the number of sub-arrays, we add that element to the result vector.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. We create an `unordered_map<int, int> map` to store the frequency of each element.\n1. We traverse the `nums` 2D array and for each element in each sub-array, we increment its frequency in `map`.\n1. We traverse the `map` and for each element whose frequency is equal to the number of sub-arrays, we add that element to the result vector.\n1. We sort the result vector in ascending order and return it.\n\n# Complexity\n- Time complexity: $$O(n log n)$$, We traverse the nums 2D array once and then the map hashmap once. Both of these operations take O(n) time, where n is the total number of elements in all sub-arrays of nums. Sorting the result vector takes O(k log k) time, where k is the number of elements in the result vector. Since k can be at most equal to the number of elements in any sub-array of nums, which is also equal to n, the time complexity of this algorithm is O(n log n).\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n)$$, We store the frequency of each element in the map hashmap, which can contain at most all the elements in all sub-arrays of nums. Therefore, the space complexity of this algorithm is O(n).\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    vector<int> intersection(vector<vector<int>>& nums) {\n        unordered_map<int, int> map;\n        for(const auto &arr: nums) {\n            for(const auto &num: arr) {\n                map[num]++;\n            }\n        }\n        int n = nums.size();\n        vector<int> res;\n        for(const auto &it: map) {\n            if(it.second == n) {\n                res.push_back(it.first);\n            }\n        }\n        sort(res.begin(), res.end());\n        return res;\n    }\n};\n```\n```java []\nclass Solution {\n    public List<Integer> intersection(int[][] nums) {\n        Map<Integer, Integer> map = new HashMap<>();\n        for (int[] arr : nums) {\n            for (int num : arr) {\n                map.put(num, map.getOrDefault(num, 0) + 1);\n            }\n        }\n        int n = nums.length;\n        List<Integer> res = new ArrayList<>();\n        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {\n            if (entry.getValue() == n) {\n                res.add(entry.getKey());\n            }\n        }\n        Collections.sort(res);\n        return res;\n    }\n}\n```
['Array', 'Hash Table', 'Sorting', 'C++', 'Java']