Thread: How to find duplicates in array of structures?

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    9

    How to find duplicates in array of structures?

    Hello,

    I have an array of structures with structure def that looks like.

    Code:
    struct {
    char * name;
    char * school;
    int age;
    }
    there are multiple people with same name but different ages so i want to list them like.

    Name.
    age 1
    age 2 and so on

    The array is sorted by name already.Any ideas?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If you want to find duplicates by name, then it is easy: duplicates will be adjacent to each other since the array is already sorted, so just make a single pass through the array.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2014
    Posts
    9
    Quote Originally Posted by laserlight View Post
    If you want to find duplicates by name, then it is easy: duplicates will be adjacent to each other since the array is already sorted, so just make a single pass through the array.
    I'm doing that but for some reason the names get printed multiple times.

    it looks like this..
    Code:
    for (i = 0; i <size; i++)
        {
            if(d[i].sName == d[i+1].sName)
            {
                printf("%20s\n%-10s%10d\n%-10s%10d\n\n",d[i].sName,d[i].school,d[i].age,d[i+1].school,d[i+1].age);
    
    
            }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If you just compare the current with the next, then you will end up printing more than once should there be a triplicate. Also, you need to account for the fact that the last element has no next element (i.e., you may be accessing the array out of bounds).

    Rather, keep track of the current "first duplicate" and use a boolean variable to keep track of whether or not a duplicate has been found. If the current element is a duplicate of this "first duplicate", you set the boolean variable and skip it. Otherwise, you print the "first duplicate" if the boolean variable is set, and then set the current element to be the new "first duplicate" and unset the boolean variable.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing duplicates found in an array?
    By TheDragonAce in forum C Programming
    Replies: 8
    Last Post: 05-09-2013, 11:05 PM
  2. Binary File-how do you find duplicates?
    By wacky_jay in forum C Programming
    Replies: 6
    Last Post: 10-06-2012, 06:19 AM
  3. Replies: 5
    Last Post: 03-29-2012, 02:57 AM
  4. Removing Duplicates from an Array
    By SlayerBlade in forum C Programming
    Replies: 4
    Last Post: 10-02-2005, 05:46 PM
  5. Array with at most n duplicates
    By kratz in forum C++ Programming
    Replies: 19
    Last Post: 07-16-2005, 11:46 PM

Tags for this Thread