How to flatten a 2D array into a sorted one-dimensional array with unique values in C

2 Answers

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

#define ROWS 3
#define COLS 4
#define SIZE ROWS * COLS

int RemoveDuplicates(int arr[], int size) {
    for (int i = 0; i < size; i++) {
        for (int j = i + 1; j < size; j++) {
            if (arr[i] == arr[j]) {
                // Delete the current duplicate element 
                for (int k = j; k < size - 1; k++) {
                    arr[k] = arr[k + 1];
                }
                size--;
                j--; // don't increment
            }
        }
    }
      
    return size;
}

int compare(const void *a, const void *b) {
    return (*(int*)a - *(int*)b);
}

void Flatten2DArrayIntoSorted1DArray(int array2d[][COLS], int rows, int cols, int arr[]) {
    int index = 0;

    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) {
            if (array2d[i][j] != 0) {
                arr[index++] = array2d[i][j];
            }
        }
    }

    qsort(arr, index, sizeof(int), compare);
}


int main() {
    int array2d[][COLS] = {
        {4, 5, 9, 3},
        {1, 4, 5, 1},
        {7, 5, 6, 8},
    };
    
    int arr[SIZE];
    
    Flatten2DArrayIntoSorted1DArray(array2d, ROWS, COLS, arr);

    int size = RemoveDuplicates(arr, SIZE);
      
    for (int i = 0; i < size; i++) {
        printf("%d, ", arr[i]);
    }

    return 0;
}



/*
run:

1, 3, 4, 5, 6, 7, 8, 9, 

*/

 



answered Aug 15, 2024 by avibootz
edited 8 hours ago by avibootz
0 votes
#include <stdio.h>
#include <stdlib.h>

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

/* 
   Function that flattens a 2D array, sorts it,
   removes duplicates, and returns the number of unique values.
*/
int flatten_sort_unique(
    int array2d[][10],   /* 2D array (fixed max columns for simplicity) */
    int rows,            /* number of rows */
    int cols[],          /* number of elements in each row */
    int *out             /* output buffer for unique sorted values */
) {
    int temp[128] = { 0 };      /* temporary buffer for flattening */
    int count = 0;

    /* 1. Flatten the 2D array into temp[] */
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols[i]; j++) {
            temp[count++] = array2d[i][j];
        }
    }

    /* 2. Sort the flattened array */
    qsort(temp, count, sizeof(int), compare_ints);

    /* 3. Remove duplicates into out[] */
    int unique_count = 0;
    for (int i = 0; i < count; i++) {
        if (i == 0 || temp[i] != temp[i - 1]) {
            out[unique_count++] = temp[i];
        }
    }

    return unique_count;
}

int main() {
    /* Input 2D array */
    int array2d[4][10] = {
        {4, 3, 3, 2},
        {30, 10, 9},
        {10},
        {1, 1, 6, 7, 7, 7, 8}
    };

    /* Number of elements in each row */
    int cols[] = {4, 3, 1, 7};

    int out[128] = { 0 }; /* buffer for unique sorted values */

    int unique_count = flatten_sort_unique(array2d, 4, cols, out);

    /* Print sorted unique values */
    for (int i = 0; i < unique_count; i++) {
        printf("%d ", out[i]);
    }

    return 0;
}



/*
run:

1 2 3 4 6 7 8 9 10 30 

*/

 



answered 8 hours ago by avibootz

Related questions

...