public int longestConsecutive(int[] nums) {
        if (nums == null) return 0;

        HashSet<Integer> set = new HashSet<Integer>();
        for (int n : nums) set.add(n);

        int maxCnt = 1;
        for (int n : nums) {
            int cnt = 1;
            int left = n - 1, right = n + 1;
            while (set.contains(left)) {
                set.remove(left);
                ++cnt;
                --left;
            }
            while (set.contains(right)) {
                set.remove(right);
                ++cnt;
                ++right;
            }
            if (cnt > maxCnt) maxCnt = cnt;
        }
        return maxCnt;
    }