Home > Blockchain >  how can I remove duplicats by row in 2d array c?
how can I remove duplicats by row in 2d array c?

Time:12-26

I have an int array which contains ascii code of every letter of words that user enters, than I need to remove dublicates, but its just dont work. I tried sorting and then removing, but I don't even know if it's really neccesary and maybe there is an easier approach. I want so that my array that contained for example:

127, 113, 127, 127, 109, 0, 0, ...;
105, 109, 114, 105, 0, 0, ...;
102, 101, 101, 101, 0, 0, 0, ....;
102, 0, 0, ....;

look like this:
127, 113, 109, 0, 0, ...;
105,109, 114,105, 0, 0, ...;
102, 101, 0, 0, ...;
102, 0, 0, ...;

I tried to replace element that is the same as j with 0, but it seems to not be working

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdbool.h>
#define N 5
#define n 50

int main(void) {
    char arr[N][10] = { 0 };
    int arri[n][n] = { 0 };
    printf("Programm to check if words are anagrams.");
    printf("\nPlease enter your word one by one:\n");
    int y = 0, u = 0;
    for (y = 0; y < N; y  )
    {
        scanf("%s", arr[y]);
    }
        int i = 0, j = 0, k = 0,g=0, num = 0, l = 0, h = 0;
        char *p;
        char *lk;
        int count1 = 0;

        for (;k<N ; l = 0, h  , k  , j = 0, i = 0)
            for (p = &arr[k][j]; *p != '\0'; l  ) //convert char into int
            {
                p = &arr[k][j];
                arri[h][i] = int(*p);
                i  ;
                j  ;
            }
        
        for (j = 0;i<n; i  )
        {
            for (;j<n;j  )
            {
                int k = j   1;
                if (arri[i][j] == arr[i][k] && arri[i][k] != 0)
                {
                    arri[i][k] = 0; // not chaning the same element with 0
                }
                
            }
        }
    
}

CodePudding user response:

To remove duplicates from an array of ASCII codes, you can use the following approach:

  1. Sort the array. This will group all of the duplicates together, making it easier to remove them.
  2. Iterate through the array and keep track of the current element and the number of times it has occurred so far.
  3. If the current element is the same as the previous element, increment the count. If it is different, reset the count to 1.
  4. If the count is greater than 1, skip the current element. Otherwise, copy the current element to the next available position in a new array.
  5. When you reach the end of the array, the new array will contain all of the unique elements from the original array.

Here is some example code that demonstrates this approach:

#include <stdio.h>
#include <stdlib.h>

// Compare function for qsort
int compare(const void *a, const void *b) {
    return *(int*)a - *(int*)b;
}

int main() {
    // Input array
    int arr[] = {127, 113, 127, 127, 109, 0, 0, 105, 109, 114, 105, 0, 0, 102, 101, 101, 101, 0, 0, 0, 102, 0, 0};

// Sort the array
qsort(arr, sizeof(arr) / sizeof(int), sizeof(int), compare);

// Output array
int out[sizeof(arr) / sizeof(int)];

// Index into the output array
int outIndex = 0;

// Current element and count
int current = arr[0];
int count = 1;

// Iterate through the array
for (int i = 1; i < sizeof(arr) / sizeof(int); i  ) {
    // If the current element is the same as the previous element, increment the count
    if (arr[i] == current) {
        count  ;
    }
    // Otherwise, reset the count to 1
    else {
        count = 1;
        current = arr[i];
    }

    // If the count is 1, copy the current element to the output array
    if (count == 1) {
        out[outIndex] = current;
        outIndex  ;
    }
}

// Print the output array
for (int i = 0; i < outIndex; i  ) {
    printf("%d ", out[i]);
}
printf("\n");

return 0;
}

CodePudding user response:

OP variables used: y, u, i, j, k, g, num, l, h, p, lk, count1
Can anyone see a problem with this?

Keep things simple. The following 'compacts' an array (a C string benefiting from the trailing '\0') into itself, removing 2nd and subsequent occurrences of any character. Apart from the array itself, this involves only two variables: 'i' & 'j'.

The trick is to realise that, when considered alone, the 0th character is obviously the 1st occurrence of that character. The first nested loops scan for subsequent occurrences of each character in the string. Those 'duplicates' are replaced with copies of the 0th character. The second loop merely 'compacts-out' all of those duplications of the 0th character.

int main( void ) {
    char arr[] = "nn-n-ow is the time for all good men to come to the aid of the party";

    puts( arr );
    int i, j;
    for( i = 1; arr[i]; i   )
        for( j = i 1; arr[j]; j   )
            if( arr[j] == arr[i] ) // repeat?
                arr[j] = arr[0];   // replace!
    for( i = 1, j = 1; arr[j]; j   )
        if( arr[j] != arr[0] )
            arr[i  ] = arr[j]; // compact
    arr[i] = '\0'; // terminate
    puts( arr );

    return 0;
}
nn-n-ow is the time for all good men to come to the aid of the party
n-ow isthemfralgdcpy

Note: This is for demonstration purposes. A unsigned char * pointer could be defined to stand-in for the literal arr[] to deal with these 1 byte integer values if desired. For this demo, a C string of ASCII characters was the most convenient representation.

A trivial optimisation would be to 'skip over' instances of the 0th character during the scan and not continue to scan for more occurrences. (ie: the outer loop could 'short cut' to avoid running the inner loop.)

  • Related