Re: Programming Puzzle
Jerry Coffin wrote:[color=blue][color=green]
> > Q7 Remove duplicates in array[/color]
>
> You can't really "remove" an element from an array, so this is poorly
> defined. If it was a C++ vector (for example) std::sort and
> std::unique would render it trivial, as would inserting the elements
> into an std::set, and then copying them back out. Doing it quickly
> while retaining the original order is a little more challenging.[/color]
How can you _not_ remove an element from an array?
Here is a trivial case:
size_t count = 2;
int * array = (int *)malloc(sizeof (int) * count);
array[0] = 42;
array[1] = 9000;
array = (int *)realloc(array , sizeof(int) * (--count));
Are you saying that an element hasn't been removed?
Jerry Coffin wrote:[color=blue][color=green]
> > Q7 Remove duplicates in array[/color]
>
> You can't really "remove" an element from an array, so this is poorly
> defined. If it was a C++ vector (for example) std::sort and
> std::unique would render it trivial, as would inserting the elements
> into an std::set, and then copying them back out. Doing it quickly
> while retaining the original order is a little more challenging.[/color]
How can you _not_ remove an element from an array?
Here is a trivial case:
size_t count = 2;
int * array = (int *)malloc(sizeof (int) * count);
array[0] = 42;
array[1] = 9000;
array = (int *)realloc(array , sizeof(int) * (--count));
Are you saying that an element hasn't been removed?
Comment