C++ || Hashmap || Clean and easy to understand
\n\n(Honestly, the first thing that striked me was sorting , but in a second I realised that it needs frequency of all the numbers and hence we are using hashma
\n\n(Honestly, the first thing that striked me was sorting , but in a second I realised that it needs frequency of all the numbers and hence we are using hashmap) \n\nWe are storing the frequency of each number in a hashmap and in another loop we are simply checking whether that number is lonely or not by using the two conditions:\n1. Count of that number is 1\n2. No adjacent digit i.e n+1 and n-1 is present in our array.\n\n\nTime complexity: **O(n),** n=number of elements in the array\nSpace complexity: **O(P)**, P= number of unique elements in the array.\n\n**Code**\n```\nclass Solution {\npublic:\n    vector<int> findLonely(vector<int>& nums) {\n        unordered_map<int,int>mp;\n        vector<int>ans;\n        for(int n:nums) mp[n]++;   // storing the frequency of all the numbers in the array\n        for(pair<int,int>n:mp){\n            int x=n.first,y=n.second;\n            if(y==1 and mp.count(x+1)==0 and mp.count(x-1)==0){   // if count of that number is 1 and no adjacent number is present then it is a lonely number \n                ans.push_back(x);\n            }\n        }\n        return ans;\n    }\n};\n```
['C++']