public static int longestConsecutive2(int[] num) {
        if (num.length == 0 || num == null) {
            return 0;
        }
        HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
        for (int value : num) {
            map.put(value, 0);
        }
        int maxlen = 1;
        for (int value : num) {
            if (map.get(value) == 1) {
                continue;
            }
            int templen = 1;
            int temp = value;//expand upward
            while (map.containsKey(temp + 1)) {
                templen++;
                temp++;
                map.put(temp, 1);
            }
            temp = value;//expand downward
            while (map.containsKey(temp - 1)) {
                templen++;
                temp--;
                map.put(temp, 1);
            }
            if (templen > maxlen) {
                maxlen = templen;
            }
        }
        return maxlen;
    }