Thread: Delete duplicate items in an array

  1. #1
    Registered User
    Join Date
    Jun 2005

    Delete duplicate items in an array

    How can i delete duplicate items in a characters' array ?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Northern Virginia/Washington DC Metropolitan Area
    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

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    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.)

    Hope is the first step on the road to disappointment.

  4. #4
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    wow if you want a more indepth answer try harder on asking the question.

  5. #5
    Join Date
    May 2005
    Sounds like an interview question. :-P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Delete instance of a class array
    By wbeasl in forum C++ Programming
    Replies: 6
    Last Post: 10-22-2007, 07:56 AM
  2. delete and delete []
    By Lionel in forum C++ Programming
    Replies: 8
    Last Post: 05-19-2005, 01:52 PM
  3. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  4. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  5. How do I remove duplicate entries in an array?
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 06-18-2002, 09:49 AM