How to resolve the algorithm Remove duplicate elements step by step in the C programming language
How to resolve the algorithm Remove duplicate elements step by step in the C programming language
Table of Contents
Problem Statement
Given an Array, derive a sequence of elements in which all duplicates are removed. There are basically three approaches seen here:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Remove duplicate elements step by step in the C programming language
Source Code 1: Linked List-Based
This code removes duplicates from an array by creating a linked list of unique elements. It iterates over the array and adds each element to the linked list if it's not already present.
Source Code 2: In-Place Array-Based (Element Swap)
This code modifies the original array in place by swapping non-unique elements to the end of the array. It maintains a count of unique elements (m
) and moves unique elements to the beginning of the array.
Source Code 3: Out-Place Array-Based (New Array Allocation)
This code is an out-place version of the previous code. It allocates a new array, copies the unique elements from the original array, and returns a pointer to the new array.
Source Code 4: qsort-Based
This code leverages the qsort
function (built-in in the C standard library) to sort the array in ascending order. It then iterates over the sorted array and skips over duplicate elements.
Common Functionality:
All four source codes achieve the same goal of removing duplicates from an array. They differ in their algorithms, memory management, and efficiency.
Efficiency:
- Source Code 1 (Linked List): O(n^2) time complexity due to nested loops.
- Source Code 2 (In-Place Swap): O(n^2) time complexity due to nested loops.
- Source Code 3 (Out-Place New Array): O(n^2) time complexity due to nested loops.
- Source Code 4 (qsort): O(n log n) time complexity due to sorting.
Memory Usage:
- Source Code 1 (Linked List): Dynamic memory allocation for each linked list node.
- Source Code 2 (In-Place Swap): No additional memory allocation.
- Source Code 3 (Out-Place New Array): Dynamic memory allocation for the new array.
- Source Code 4 (qsort): No additional memory allocation (uses the original array).
Source code in the c programming language
#include <stdio.h>
#include <stdlib.h>
struct list_node {int x; struct list_node *next;};
typedef struct list_node node;
node * uniq(int *a, unsigned alen)
{if (alen == 0) return NULL;
node *start = malloc(sizeof(node));
if (start == NULL) exit(EXIT_FAILURE);
start->x = a[0];
start->next = NULL;
for (int i = 1 ; i < alen ; ++i)
{node *n = start;
for (;; n = n->next)
{if (a[i] == n->x) break;
if (n->next == NULL)
{n->next = malloc(sizeof(node));
n = n->next;
if (n == NULL) exit(EXIT_FAILURE);
n->x = a[i];
n->next = NULL;
break;}}}
return start;}
int main(void)
{int a[] = {1, 2, 1, 4, 5, 2, 15, 1, 3, 4};
for (node *n = uniq(a, 10) ; n != NULL ; n = n->next)
printf("%d ", n->x);
puts("");
return 0;}
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
/* Returns `true' if element `e' is in array `a'. Otherwise, returns `false'.
* Checks only the first `n' elements. Pure, O(n).
*/
bool elem(int *a, size_t n, int e)
{
for (size_t i = 0; i < n; ++i)
if (a[i] == e)
return true;
return false;
}
/* Removes the duplicates in array `a' of given length `n'. Returns the number
* of unique elements. In-place, order preserving, O(n ^ 2).
*/
size_t nub(int *a, size_t n)
{
size_t m = 0;
for (size_t i = 0; i < n; ++i)
if (!elem(a, m, a[i]))
a[m++] = a[i];
return m;
}
/* Out-place version of `nub'. Pure, order preserving, alloc < n * sizeof(int)
* bytes, O(n ^ 2).
*/
size_t nub_new(int **b, int *a, size_t n)
{
int *c = malloc(n * sizeof(int));
memcpy(c, a, n * sizeof(int));
int m = nub(c, n);
*b = malloc(m * sizeof(int));
memcpy(*b, c, m * sizeof(int));
free(c);
return m;
}
int main(void)
{
int a[] = {1, 2, 1, 4, 5, 2, 15, 1, 3, 4};
int *b;
size_t n = nub_new(&b, a, sizeof(a) / sizeof(a[0]));
for (size_t i = 0; i < n; ++i)
printf("%d ", b[i]);
puts("");
free(b);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int icmp(const void *a, const void *b)
{
#define _I(x) *(const int*)x
return _I(a) < _I(b) ? -1 : _I(a) > _I(b);
#undef _I
}
/* filter items in place and return number of uniques. if a separate
list is desired, duplicate it before calling this function */
int uniq(int *a, int len)
{
int i, j;
qsort(a, len, sizeof(int), icmp);
for (i = j = 0; i < len; i++)
if (a[i] != a[j]) a[++j] = a[i];
return j + 1;
}
int main()
{
int x[] = {1, 2, 1, 4, 5, 2, 15, 1, 3, 4};
int i, len = uniq(x, sizeof(x) / sizeof(x[0]));
for (i = 0; i < len; i++) printf("%d\n", x[i]);
return 0;
}
You may also check:How to resolve the algorithm Lucas-Lehmer test step by step in the Julia programming language
You may also check:How to resolve the algorithm Extreme floating point values step by step in the Perl programming language
You may also check:How to resolve the algorithm Roots of a quadratic function step by step in the Pascal programming language
You may also check:How to resolve the algorithm Currying step by step in the Wren programming language
You may also check:How to resolve the algorithm Combinations step by step in the IS-BASIC programming language