public int longestConsecutive(int[] num) {
        if (num == null || num.length == 0) {
            return 0;
        } else if (num.length == 1) {
            return 1;
        }
        Set<Integer> set = new HashSet<Integer>();
        for (int v : num) {
            set.add(v);
        }
        int max = 1;
        for (int v : num) {
            if (!set.contains(v)) {
                continue;
            }
            int x = v, n = 1;
            while (set.contains(--x)) {
                set.remove(x);
                n++;
            }
            x = v;
            while (set.contains(++x)) {
                set.remove(x);
                n++;
            }
            if (max < n) {
                max = n;
            }
        }
        return max;
    }