[Tutor] Uniques values in a list

Sean 'Shaleh' Perry shalehperry@attbi.com
Sat, 16 Mar 2002 07:45:51 -0800 (PST)


> 
> 
> => Do you know any other solution (eg. using functional programming or 
> similar woodoo)?
> 

the "standard" approach is to sort the list, then walk it.  You hold the first
index in your hand and look at the second.  If they match you dump the second
and move on to the third.  You continue until a non-match is found.  you then
hold the new item and look at the next one.

This is from the g++ 3.0 stl.

void list::unique
{
  iterator __first = begin();
  iterator __last = end();
  if (__first == __last) return;
  iterator __next = __first;
  while (++__next != __last) {
    if (*__first == *__next)
      erase(__next);
    else
      __first = __next;
    __next = __first;
  }
}

It does an in place cleaning.