public int longestConsecutive_2(int[] num) {
        // Note: The Solution object is instantiated only once and is reused by
        // each test case.
        if(num == null || num.length == 0){
            return 0;
        }
        HashSet<Integer> set = new HashSet<Integer>();
        int maxLen = 1;
        for(int n : num) set.add(n);
        int[] plusMinus = new int [] {-1, 1};
        for(int n : num){
            if(set.contains(n)){
                int tmpLen = 1;
                for(int one : plusMinus){
                    int m = n+one;
                    while (set.contains(m)){
                        ++tmpLen;
                        set.remove(m);
                        m += one;
                    }
                }
                maxLen = Math.max(maxLen, tmpLen);
                set.remove(n);
            }
        }
        return maxLen;
    }