What is the method to remove dupplicate items from an character array??

A Sample Program is Here which take two arrays merge them and sort them please include some lines that should remove duplicates from an resulted array??

#include <stdio.h>

  
// A simple bubble sort
void sort(char data[], int length)
{
   int end = length - 1;
   for (int i = 0; i < length; ++i) 
   {
       for (int j = 0; j < end; ++j) 
       {
           if (data[j] > data[j+1])
           {
               char tmp = data[j];
               data[j] = data[j+1];
               data[j+1] = tmp;
           }
       }
       --end;
   }
}

int main(void)
{
   char firstArray[10];
   char secondArray[10];
   char mergedArray[20];

   int i;
   int mergedIndex;

   printf("Put in the first array\n");
   fgets(firstArray, sizeof(firstArray), stdin);

   printf("Put in the second array\n");
   fgets(secondArray, sizeof(secondArray), stdin);

   // Copy the first array into the merged array
   for(i = 0; firstArray[i] != '\n'; ++i)
   {
       mergedArray[i] = firstArray[i];
   }
   mergedIndex = i;
   for(i = 0; secondArray[i] != '\n'; ++i)
   {
       mergedArray[mergedIndex++] = secondArray[i];
   }
   mergedArray[mergedIndex] = 0;

   sort(mergedArray, mergedIndex);
   printf("Merged array is '%s'\n", mergedArray);
   return 0;
}

Thanks inAdvance

Recommended Answers

All 2 Replies

You could sort the array and the check if adjacent values are the same.

if the dataarray is str[100]
str1 is the resultant array then
I think, The following code works only if the array has characters other than '0'.

for(i=0,k=0;str[i] != '\0' ; i++)
{
     if(str[i] != '0')
     {
          str1[k] = str[i];
           k++;
            for (j=i; str[j] != '\0'  ; j++)
                 if(str[j] == str[i])
                       str[j] = '0'; 
       }
}

What is the method to remove dupplicate items from an character array??

A Sample Program is Here which take two arrays merge them and sort them please include some lines that should remove duplicates from an resulted array??

#include <stdio.h>

  
// A simple bubble sort
void sort(char data[], int length)
{
   int end = length - 1;
   for (int i = 0; i < length; ++i) 
   {
       for (int j = 0; j < end; ++j) 
       {
           if (data[j] > data[j+1])
           {
               char tmp = data[j];
               data[j] = data[j+1];
               data[j+1] = tmp;
           }
       }
       --end;
   }
}

int main(void)
{
   char firstArray[10];
   char secondArray[10];
   char mergedArray[20];

   int i;
   int mergedIndex;

   printf("Put in the first array\n");
   fgets(firstArray, sizeof(firstArray), stdin);

   printf("Put in the second array\n");
   fgets(secondArray, sizeof(secondArray), stdin);

   // Copy the first array into the merged array
   for(i = 0; firstArray[i] != '\n'; ++i)
   {
       mergedArray[i] = firstArray[i];
   }
   mergedIndex = i;
   for(i = 0; secondArray[i] != '\n'; ++i)
   {
       mergedArray[mergedIndex++] = secondArray[i];
   }
   mergedArray[mergedIndex] = 0;

   sort(mergedArray, mergedIndex);
   printf("Merged array is '%s'\n", mergedArray);
   return 0;
}

Thanks inAdvance

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.