Two Pointer Technique

a popular programming technique used to solve problems involving arrays or lists efficiently. It involves using two pointers (or indices) to traverse the data structure and is commonly used in problems related to searching, sorting, and subarray operations. It Typically can reduces the time complexity from $O(n^2)$ to $O(n)$ for many problems. - ChatGPT

Example

template<class ForwardIt>
ForwardIt unique(ForwardIt first, ForwardIt last)
{
    if (first == last)
        return last;
 
    ForwardIt result = first;
    while (++first != last)
        if (!(*result == *first) && ++result != first)
            *result = std::move(*first);
 
    return ++result;
}

see also

Written on November 17, 2024, Last update on November 17, 2024
algorithm sort