```
int findLHS(vector<int>& nums) {
unordered_map<int, int> hash;
int ans = 0;
for(int n : nums){
hash[n]++;
if(hash[n-1] != 0) ans = max(ans, hash[n] + hash[n-1]);
if(hash[n+1] != 0) ans = max(ans, hash[n] + hash[n+1]);
}
return ans;
}
```