HashSet
and a variable count
to 0.arr
to the HashSet
to remove duplicates and allow for O(1)
lookups.arr
.x
in arr
, check if x + 1
exists in the HashSet
.x + 1
is found in the HashSet
, increment count
by 1.count
as the result.