educative.io

Educative

Preventing Duplicate Triplet is redundant

Solutions has both of these. However, only having either one will prevent the duplicate triplet. Can you give an example that can be caught one case and not in the other?

  if (i > 0 && arr[i] == arr[i - 1]) // skip same element to avoid duplicate triplets
    continue;

and

    while (left < right && arr[left] == arr[left - 1])
      left++; // skip same element to avoid duplicate triplets
    while (left < right && arr[right] == arr[right + 1])
      right--; // skip same element to avoid duplicate triplets

Hi @LPB, thanks for reaching out.
Actually, it is not redundant but an efficient way of skipping the elements while checking from both sides. As we are traversing from both sides; from left and right simultaneously so we have to skip elements from both sides.