public int longestConsecutive(int[] num) {

    Set<Integer> set = new HashSet<Integer>();
    for (int i : num) {
      set.add(i);
    }

    int max = 0;
    for (int curt : num) {

      int left = curt - 1;
      int right = curt + 1;
      int count = 1;

      while (set.contains(left)) {
        set.remove(left); // save some time, will TLE if not remove this
        left--;
        count++;
      }

      while (set.contains(right)) {
        set.remove(right);
        right++;
        count++;
      }

      if (count > max) {
        max = count;
      }
    }

    return max;
  }