```
int findLHS(vector<int>& nums) {
if (nums.empty()) return 0;
int result = 0;
unordered_map<int, int> mp;
for (const auto& n : nums) {
++mp[n];
}
for (auto i = mp.begin(); i != mp.end(); ++i) {
if (mp.find(i->first + 1) != mp.end())
result = max(result, i->second + mp[i->first + 1]);
}
return result;
}
```