- run time O(n) space O(n) use unordered_map

First loop through all elements and count each number appearance. Then loop through unordered_map, to find if the key - 1 is in the unordered map. If key - 1 and key both in the map, update the result

```
int findLHS(vector<int>& nums) {
unordered_map<int,int>m;
for(auto i: nums)
m[i]++;
int res = 0;
for(auto it:m)
if(m.count(it.first-1)>0)
res = max(res, it.second+m[it.first-1]);
return res;
}
```

- O(nlogn) running time ,space O(1) using sort

The idea is to loop through each elements and update the result. The start position is used for counting purpose and new start is used for whenever come across different number

When the number is different from previous number, update the new start position. When difference between current position and start position is bigger than 1 then update start position.

```
int findLHS(vector<int>& nums) {
sort(nums.begin(),nums.end());
int len = 0;
for(int i = 1, start = 0, new_start = 0; i<nums.size(); i++)
{
if (nums[i] - nums[start] > 1)
start = new_start;
if (nums[i] != nums[i-1])
new_start = i;
if(nums[i] - nums[start] == 1)
len = max(len, i-start+1);
}
return len;
```