Let `count[x]`

be the number of `x`

's in our array.

Suppose our longest subsequence `B`

has `min(B) = x`

and `max(B) = x+1`

.

Evidently, it should use all occurrences of `x`

and `x+1`

to maximize it's length, so `len(B) = count[x] + count[x+1]`

.

Additionally, it must use `x`

and `x+1`

atleast once, so `count[x]`

and `count[x+1]`

should both be positive.

```
def findLHS(self, A):
count = collections.Counter(A)
ans = 0
for x in count:
if x+1 in count:
ans = max(ans, count[x] + count[x+1])
return ans
```

Alternatively, we can count values in a straightforward way using a dictionary: replacing our first line of `count = collections.Counter(A)`

with:

```
count = {}
for x in A:
count[x] = count.get(x, 0) + 1
```