[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: help with C algorythm (find unique value in an array) could you please make changes



No you're quite right, not least since my insertion routine
traverses the whole list on each call. I didn't write this
for speed of execution, merely speed of writing it :)

For speed of writing, here's the whole thing in C++ using STL:

#include <vector>
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
 srand(time(NULL));
 vector<int> array;                        // original array
 vector<int> unique;                     // each unique element from
original included only once
 enum {SIZE=100};
 for (int i=0; i < SIZE; ++i)            // populate with random ints
   array.push_back(rand()%50);

 // remove duplicates:
 sort(array.begin(),array.end());
 unique_copy(array.begin(), array.end(), back_inserter(unique));

 cout <<"Unique size: " <<unique.size() <<endl;
 return 0;
}



Reply to: