Sort✅O(nlogn) || HashMap O(N)
Intuition\nThe problem seems to involve finding numbers in the array that are not adjacent to any other numbers. We can achieve this by sorting the array and th
# Intuition\nThe problem seems to involve finding numbers in the array that are not adjacent to any other numbers. We can achieve this by sorting the array and then iterating through it to check if each number is not adjacent to its neighboring numbers.\n\n# Approach\n**Sort**\n- Sort the input array nums.\n- Iterate through the sorted array:\n- Check if the current number is not adjacent to the previous number (nums[i]-nums[i-1]>1) as we know the adjacent numbers have the diff 1, if duplicates exists then the diff would be 0 ,so we apply the condition >1 ,if the diff is only >1 then it means they are not adjacent \n- Similaryly checking for the curr no and next no(nums[i + 1] - nums[i] > 1)\n- i==0 and i==n-1 are the boundary conditions to be taken care of \n- If it\'s not adjacent to any other number, add it to the result list l.\n- Return the list l containing the lonely numbers.\n\n**HASHMAP**\n\n1. Count Occurrences: Iterate through the array and store the frequency of each number in a HashMap.\n2. Identify Lonely Numbers: Iterate through the array again. For each number, check if its frequency is 1 and if there are no occurrences of num - 1 or num + 1 in the HashMap.\n3. Return Result: Return the list of lonely numbers.\n# Complexity\n- Time complexity:\n- Sorting -O(nlogn)\n- Hashmap-O(n)\n\n- Space complexity:O(n)\n\n# Code\n```\nclass Solution {\n    public List<Integer> findLonely(int[] nums) {\n        Arrays.sort(nums); \n        List<Integer> l = new ArrayList<>();\n\n        int n = nums.length;\n       for (int i = 0; i < n; i++) {\n            if ((i == 0 || nums[i] - nums[i - 1] > 1) && //curr no is not adjacent to the previous number\n                (i == n - 1 || nums[i + 1] - nums[i] > 1)) { // Check if the curr no is not adjacent to nxt no\n                l.add(nums[i]);\n            }\n        }\n\n        return l;\n    }\n}\n//HASHMAP\nclass Solution {\n    public List<Integer> findLonely(int[] nums) {\n        Map<Integer, Integer> m = new HashMap<>();\n        List<Integer> res = new ArrayList<>();\n\n        for (int num : nums) {\n            m.put(num,m.getOrDefault(num, 0) + 1);\n        }\n        for (int num : nums) {\n            if (m.get(num) == 1 && !m.containsKey(num - 1) && !m.containsKey(num + 1)) {\n                res.add(num);\n            }\n        }\n        return res;\n    }\n}\n\n```
['Java']