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.
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
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