Dan said:
Victor Bazarov said:
Dan said:
I would to know if it is possible to delete an instance in an array, The
following does not allow me to do a delete.
I am trying to find and delete the duplicate in an array, thanks
What would you expect the result to be? Say, I have an array
to begin with:
int int_array[] = { 1, 2, 3, 4, 3, 2 };
and I want to delete the duplicate '2'. What should the array
be after I finish "deleting" from it?
Yes the idea was to delete only the duplicate entry,
so the above would be 1,2,3,4.
But that has a problem because the size of the array would change.
So then the second step would be creating a dynamic array.
But now I cannot seem to delete that duplicate value.
I would stop uses the term delete, as I'm sure you know delete refer to
something quite different in C++ from what you are trying to do. Lets just
say you are trying to remove duplicate entries. And the only way to do that
is to overwrite entries with other ones. For instance
1, 2, 3, 2, 4, 5
goes to
1, 2, 3, 4, 5, 5
The second 2 has been removed by copying all the higher elements (4 and 5)
down one position. Now of course we have an extra 5 at the end, but that
doesn't matter because we are going to shorten the array by one, so that
extra 5 will be removed when that happens.
Some questions
1) Are you prepared to sort the array, or must the elements stay in the
order they are already? The algorithm you should use is simpler and more
efficient if you are prepared to sort the array (because in a sorted array
the duplicate elements are adjacent).
2) Are you prepared to use a vector? It could literally be one line of code
if you are. In a perfect world there would be no reason at all not to use a
vector for this problem, but I realise that maybe you have never used one
before and you want to stick with what you know. But in truth vectors are
easier than dynamic arrays, vectors are what newbies should use all the time
while dynamic arrays are what experts should use sometimes (most of the time
experts should use vectors as well).
john