public int longestConsecutive(int[] num) {
        if(num.length == 0) return 0;
        Set<Integer> set = new HashSet<Integer>();
        for(int n : num) set.add(n);

        int maxLen = 0;
        for(int number : num){
            int len = 1;

            int n = number + 1;
            while(set.contains(n)){
                set.remove(n);
                n++;
                len++;
            }

            n = number - 1;
            while(set.contains(n)){
                set.remove(n);
                n--;
                len++;
            }
            maxLen = len > maxLen ? len : maxLen;
        }
        return maxLen;
    }