✅ Optimized C++ Solution
Approach\nIf one goes for the below written approach , then the constraints are large enough to generate a TLE error.\n\n\n\n\n\nclass Solution {\npublic:\n// f
# Approach\nIf one goes for the below written approach , then the constraints are large enough to generate a **TLE error**.\n\n![tle.png](https://assets.leetcode.com/users/images/d3565f5c-ecaf-4c19-b9bb-32e655baaa16_1672169444.703417.png)\n\n\n```\nclass Solution {\npublic:\n// function to check if (x-1) and (x+1) is present or not\nbool check(vector<int>& nums,int n){\n    int a=n-1;\n    int b=n+1;\n    bool value = true;\n    for(int i=0;i<nums.size();++i){\n        if(nums[i]==a || nums[i]==b){\n            value=false;\n            //cout<<value<<" ";\n            break;\n        }\n    }\n    return value;\n}\n    vector<int> findLonely(vector<int>& nums) {\n     unordered_map<int,int> mp;\n     for(int i=0;i<nums.size();++i){\n         mp[nums[i]]++;\n     }   \n     vector<int> temp;\n     for(auto i:mp){\n         if(i.second==1){\n             temp.push_back(i.first);\n         }\n     }\n     vector<int> ans;\n     for(int i=0;i<temp.size();++i){\n        bool okay = check(nums,temp[i]);\n        if(okay) ans.push_back(temp[i]);\n     }\n     return ans;\n    }\n};\n```\n So an optimised approach would be , using **HASHING** as well as the **find** function which was missing in the above  approach to reduce time and space complexity thus avoiding a TLE Error which is always a nightmare !\n\n\n# Optimized Approach\n```\nclass Solution {\npublic:\n    vector<int> findLonely(vector<int>& nums) {\n     //declared a vector to store the lonely numbers\n     vector<int> ans;\n\n     //Use hashmap to count the frequency of all the elements of the vector\n      unordered_map<int,int> mp;\n      for(int i=0;i<nums.size();++i){\n         mp[nums[i]]++; \n      }  \n      \n      // Now check if the frequency of the element is 1 and the adjacent numbers x+1 and x-1 are not found (for this use find function to check whether their frequency is not present in hashmap or it is -1)\n      for(int i=0;i<nums.size();++i){\n\n          if(mp[nums[i]]==1){\n\n              if(mp.find(nums[i]+1)==mp.end() && mp.find(nums[i]-1)==mp.end())   {\n\n                 //If all the conditions are met , then push the lonely numbers into the ans vector\n                  ans.push_back(nums[i]);\n\n              }\n          }\n      }\n\n      return ans;\n\n    }\n};\n```
['C++']