Thread: I have a char * array that i need to remove duplicates

  1. #16
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Code:
    for (j = 0; j < sizeof(array)/sizeof(array[0]); j++)
    j should only run to the array size -1.
    Because, if j is the last index, there is no member behind it to check.
    should be:
    Code:
    for (j = 0; j < (sizeof(array) / sizeof(array[0]) - 1); j++)
    In the inner loop, you check if member j points to NULL, very good.
    But is it NULL, you run the rest of the array with k.
    You can check it before k is run.

    Directly in the outer loop:
    Code:
    if (array[j] != NULL) {
        int k;
        …
    }
    Line 16 and 18 are both if-statements. You can compine it.
    Now that we know that array[j] isn't NULL, we come to the resulting if-statement:
    Code:
    if (array[k] != NULL && strcmp(array[j], array[k]) == 0)
    Also please fix what laserlight says. I let you find out by your self, because it is very simple.

    All in all, nice work! Great job. Good formated and indented source, nice.
    Last edited by WoodSTokk; 05-05-2016 at 07:03 AM.
    Other have classes, we are class

  2. #17
    Registered User
    Join Date
    May 2016
    Posts
    31
    Quote Originally Posted by WoodSTokk View Post
    Code:
    for (j = 0; j < sizeof(array)/sizeof(array[0]); j++)
    j should only run to the array size -1.
    Because, if j is the last index, there is no member behind it to check.
    should be:
    Code:
    for (j = 0; j < (sizeof(array) / sizeof(array[0]) - 1); j++)
    In the inner loop, you check if member j points to NULL, very good.
    But is it NULL, you run the rest of the array with k.
    You can check it before k is run.

    Directly in the outer loop:
    Code:
    if (array[j] != NULL) {
        int k;
        …
    }
    Line 16 and 18 are both if-statements. You can compine it.
    Now that we know that array[j] isn't NULL, we come to the resulting if-statement:
    Code:
    if (array[k] != NULL && strcmp(array[j], array[k]) == 0)
    Also please fix what laserlight says. I let you find out by your self, because it is very simple.

    All in all, nice work! Great job. Good formated and indented source, nice.
    That all makes total sense. Thanks for the advice.

    So far, I have the following (with everyone's help of course):


    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main() {
    
    
        const char* array[] = { "abc", "def", "def", "ghi", "abc", "abc" };
    
    
        int j;
        for (j = 0; j < (sizeof(array)/sizeof(array[0]) - 1); j++)
        {
            if (array[j] != NULL)
            {
                int k;
                for (k = j + 1; k < sizeof(array)/sizeof(array[0]); k++)
                {        
                    if (array[k] != NULL && strcmp(array[j],array[k]) == 0)
                    {
                        array[k] = NULL;
                    }
                }
            }    
        }
    
        int a;
        for (a = 0; a < sizeof(array)/sizeof(array[0]); a++)
        {
            if (array[a] != NULL)
            {
                printf("%s \n",array[a]);
            }
        }
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Remove duplicates and save to txt
    By ///André in forum C++ Programming
    Replies: 3
    Last Post: 04-19-2016, 12:44 PM
  2. Two int Arrays, remove duplicates from second
    By Sorin in forum C Programming
    Replies: 5
    Last Post: 04-03-2013, 03:51 AM
  3. Strings and Structs (remove duplicates)
    By christianB in forum C Programming
    Replies: 44
    Last Post: 07-25-2011, 09:01 PM
  4. recursive remove duplicates from a string
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-20-2009, 02:02 AM
  5. Really basic remove duplicates from array
    By Chris Fowler in forum C++ Programming
    Replies: 7
    Last Post: 11-25-2002, 10:35 PM

Tags for this Thread