Java HashMap Approach
HashMap Approach\n## Intuition\nFrom the problem, we feel the need to have the frequency of the elements present in the given array.\nAfter we have the frequenc
# HashMap Approach\n## Intuition\nFrom the problem, we feel the need to have the frequency of the elements present in the given array.\nAfter we have the frequency of elements, we can scan through the map\'s keys to learn about the frequency of the key and get information about the presence of its consecutive elements in the map.\n\n## Algorithm\nWe will follow the following steps to solve the problem:\n**Step 1:** We will use a hashmap to store the frequency of the elements in the given array.\n**Step 2:** Traverse through the keys of the map.\n**Step 3:** For every key of the map, we check if the frequency of the current key is equal to one, and the consecutive numbers to the given key are not present in the map.\n**Step 4:** If the above conditions are satisfied, we add the current key into the ans list.\n\n## Visualisation\nConsider an example, nums = [10, 6, 5, 8, 5]\nThe HashMap will look like\n![image](https://assets.leetcode.com/users/images/1e9673bb-b223-4116-8ff0-a4a695b13b30_1643132635.9604485.png)\n\nIterating through the map\n![image](https://assets.leetcode.com/users/images/7522c902-4655-4d31-a62b-46bc7c0ae76c_1643133463.997268.png)\n![image](https://assets.leetcode.com/users/images/fe1b68f2-7081-46d2-ad19-e556fbed0c59_1643133589.7946994.png)\n![image](https://assets.leetcode.com/users/images/ae967fa9-4ec0-4a21-b06e-90686129d99d_1643133663.6053195.png)\n![image](https://assets.leetcode.com/users/images/5e662b01-417c-4d62-a601-9521fa6ca059_1643133767.9396262.png)\n\nFinal ans = [10, 8]\n\n## Implementation\n```\nclass Solution {\n    public List<Integer> findLonely(int[] nums) {\n        Map<Integer, Integer> map = new HashMap<>();\n        \n        for (int num : nums) {\n            map.put(num, map.getOrDefault(num, 0) + 1);\n        }\n        \n        List<Integer> ans = new ArrayList<>();\n\t\tfor (int num : nums) {\n            if (map.get(num) == 1 && !map.containsKey(num + 1) && !map.containsKey(num - 1)) {\n                ans.add(num);\n            }\n        }\n        return ans;\n    }\n}\n```\n\n## Complexity Analysis\n### Space Complexity\nWe use a HashMap in our solution, giving us a **O(N)** space complexity.\n\n### Time Complexity\nWe traverse the given array once, taking O(N) time.\nThen, we traverse the keys of the map; in the worst case, if all the elements in the array are distinct, it takes O(N) time.\nHence, the resulting time complexity is **O(N)**\n\n## Curiosity Question\n**Why do we traverse the keys of the map rather than traverse the array again?**\nSuppose there are ten duplicate elements present in the array, then we will have to traverse through ten elements of the array.\nInstead, if we traverse through the keys of the map, we will have to traverse only one single entry of the map.\nHence, Traversing through the keys of the map is an optimised way to solve the problem.\n\n**Happy Coding!**
['Java']