helpp

how to eliminate duplicate from a singal integer array?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Given an array, eliminates duplicates in that array and returns the new size
int RemoveDuplicates(int* arr, int size)
{
  std::sort(arr, arr+size);
  for (int i = 0; i < size-1; i++)
  {
    if ( arr[i] == arr[i+1] )
    {
      for (int j = i+1; j < size-1; j++)
        arr[j] = arr[j-1];
      --size;
    }
  } 
  return size;
}
Last edited on
std::sort + std::unique + std::distance
http://www.cplusplus.com/reference/algorithm/unique/

Without sorting one would have to keep track of the already found values.
Topic archived. No new replies allowed.