Your solution is already close to the right one. You just don't have to clone the array, just edit it in-place.
int i, j;
for (i= 1, j= 1; i < array.Length; i++)
{
if (array[i] != array[i - 1])
{
array[j]= array[i];
j++;
}
}
In the end,
j
contains the number of valid entries in
array
.
Simple and effective. You can resize the array but this involves a copy.
[I am not questioning the approach. Creating the array, sorting then purging will do the trick. This will take time proportional to N.Log(N).L for N words of L characters (on average) and require storage of N.L characters.
Using a hash table as some suggested will tend to reduce the time to N.L and the storage to N'.L, where N' denotes the number of distinct words. But there will be some overhead on time and space.
Telling the best approach would take some comparative experimentation.]