public int longestConsecutive(int[] num) {

        int result = 0;
        HashMap<Integer, Integer> h = new HashMap<Integer, Integer>();
        //put all number into the hashmap
        for (int i = 0; i < num.length; i++) {
            h.put(num[i], null);
        }
        //check the longest consecutive list number
        for (int i = 0; i < num.length; i++) {
            //at first, get one number num[i]
            int count = 1;
            //check less than num[i]
            int x = num[i] - 1;
            while (h.containsKey(x)) {
                h.remove(x);
                count++;
                x--;
            }
            //check bigger than num[i]
            int y = num[i] + 1;
            while (h.containsKey(y)) {
                h.remove(y);
                count++;
                y++;
            }
            //sum all possible consecutive sequence
            if (count > result) {
                result = count;
            }
        }
        return result;
    }