How can i delete duplicate items in a characters' array ?
This is a discussion on Delete duplicate items in an array within the C Programming forums, part of the General Programming Boards category; How can i delete duplicate items in a characters' array ?...
How can i delete duplicate items in a characters' array ?
Well, you're probably going to have to shift everything down by one position in the array starting just after the item you wish to delete. Also, if there is no way to tell where the end of the array is you'll need to have another variable that indicates the current size of the array. With character strings this isn't that important usually because of the terminating NULL that serves as a sentinel value of sorts, but with an integer array for example, you would need to keep track of the size unless you assigned a specific value as a sentinel.
[edit]Let me try that again...
Assuming an unsorted array... You need to first determine what values are duplicates so, you look at a particular value at a given index and do a search of the array up to (but not including) the current index to see if it comes up somewhere else in the array. If you find it elsewhere, you need to do the above shifting of values to overwrite the duplicate you just found.
If your array is sorted then all the like values are going to be next to eachother which makes things easier.[/edit]
Last edited by hk_mp5kpdw; 06-16-2005 at 11:46 AM.
I used to be an adventurer like you... then I took an arrow to the knee.
It's pretty much the same either way. You just find one that you want to "delete", and copy everything beyond it over top of where it is onward. Just call memmove if you don't feel like using a loop.
However, as you've stated, you still have to do something to denote the new end of the array, and denote the new "empty" array member.
Somehow though, I doubt the origional poster gave as much thought to the problem as we did. (Which I admit, wasn't a whole lot on my part, because it's a pretty simple task.)
Quzah.
Hope is the first step on the road to disappointment.
wow if you want a more indepth answer try harder on asking the question.
Sounds like an interview question. :-P