Sequence table delete duplicates

Write algorithm , In a non decreasing sequence table L in , Delete all redundant elements with equal values . The required time complexity is O(n), Space complexity is O(1).

The function prototype is as follows ：
void del_dupnum(SeqList *L)

Relevant definitions are as follows ：
struct _seqlist{ ElemType elem[MAXSIZE]; int last; }; typedef struct _seqlist
SeqList;
Provide code
#include <stdio.h> #include <stdlib.h> #include "list.h" // Please do not delete , Otherwise, the inspection fails void
del_dupnum(SeqList *L) { }

Reference code v2.0.0  By first turning the duplicate into min of , Then assign value , Meet the requirements
/* Sequence table Remove duplicates Write algorithm , In a non decreasing sequence table L in , Delete all redundant elements with equal values . The required time complexity is O(n), Space complexity is O(1). */ #include
<stdio.h> #include <stdlib.h> //#include "list.h" // Please do not delete , Otherwise, the inspection fails #define
ElemType int #define MAXSIZE 20 struct _seqlist { ElemType elem[MAXSIZE]; int
last; }; typedef struct _seqlist SeqList; void del_dupnum(SeqList* L){ int min
= L->elem[0]; int tmp = L->elem[0]; for (int i = 1; i <= L->last; i++) { if
(L->elem[i] == tmp) L->elem[i] = min; else tmp = L->elem[i]; } int p=1, q=1;
while (q <= L->last) { if (L->elem[q] != min) { L->elem[p] = L->elem[q]; p++; }
q++; } L->last = p-1; } void print(const SeqList* L) { int i = 0; for (i = 0; i
<= L->last; i++) { printf("%d ", L->elem[i]); } } int main() { SeqList L = {
{1, 2, 3, 7, 7, 8, 8, 9, 9, 10, 10, 10, 10, 10, 10, 11},15 };// test case
del_dupnum(&L); print(&L);// Print to see if the deletion is successful return 0; }

Auxiliary understanding code
#include <stdint.h> #include <stdio.h> int main() { int L[] = {1, 2, 3, 7, 7,
8, 8, 9, 9, 10, 10, 10, 10, 10, 10, 11}; int size = sizeof(L) / sizeof(int); //
Find the smallest first L[0] int min = L[0]; printf("size = %d\n", size); printf("L = ["); for
(int i = 0; i < size; i++) { printf("%d", L[i]); printf(i == size - 1 ? "]\n" :
", "); } printf(" "); for (int i = 0; i < size; i++) { if (i < 10) printf(L[i]
>= 10 ? "%d " : "%d ", i); else printf(L[i] >= 10 ? "%d " : "%d ", i); if (i ==
size - 1) puts(""); } // Only one element does not need to be executed if (size == 1) { printf("L = [%d]\n",
L[0]); return 0; } // Previous valid data int p = L[0]; for (int i = 0; i < size; i++) {
if (L[i] == p) L[i] = min; else p = L[i]; } // output printf("size = %d\n", size);
printf("L = ["); for (int i = 0; i < size; i++) { // Ignore marked if (i != 0 && L[i]
== min) continue; printf("%d", L[i]); printf(i == size - 1 ? "]\n" : ", "); }
printf(" "); for (int i = 0; i < size; i++) { // Ignore marked if (i != 0 && L[i] ==
min) continue; if (i < 10) printf(L[i] >= 10 ? "%d " : "%d ", i); else
printf(L[i] >= 10 ? "%d " : "%d ", i); if (i == size - 1) puts(""); } // Finishing up
p = 1; int q = 1; while (q < size) { if (L[q] != min) L[p++] = L[q]; q++; }
size = p; printf("size = %d\n", size); printf("L = ["); for (int i = 0; i <
size; i++) { // Ignore marked if (i != 0 && L[i] == min) continue; printf("%d", L[i]);
printf(i == size - 1 ? "]\n" : ", "); } printf(" "); for (int i = 0; i < size;
i++) { // Ignore marked if (i != 0 && L[i] == min) continue; if (i < 10) printf(L[i]
>= 10 ? "%d " : "%d ", i); else printf(L[i] >= 10 ? "%d " : "%d ", i); if (i ==
size - 1) puts(""); } return 0; }
Reference code v1.0.0 Use bubble sort , High complexity , Non conformance
/* Sequence table Remove duplicates Write algorithm , In a non decreasing sequence table L in , Delete all redundant elements with equal values . The required time complexity is O(n), Space complexity is O(1). */ /*
Code thinking ： Borrowing the idea of bubble sorting */ #include <stdio.h> #include <stdlib.h> //#include "list.h" //
Please do not delete , Otherwise, the inspection fails #define ElemType int #define MAXSIZE 10 struct _seqlist {
ElemType elem[MAXSIZE]; int last; }; typedef struct _seqlist SeqList; void
del_dupnum(SeqList* L) { int i = 0; int j = 0; while (i<L->last) { for (j = i;
j < L->last; ) { // Compare for equality if (L->elem[i] == L->elem[j + 1]) { int k = 0;
// Delete the last same for (k = j+1; k < L->last; k++) { L->elem[k] = L->elem[k + 1]; }
L->last--; } else { j++; } } i++; } } void print(const SeqList* L) { int i = 0;
for (i = 0; i <= L->last; i++) { printf("%d ", L->elem[i]); } } int main() {
SeqList L = { {0,0,0,3,3,5},4 };// test case del_dupnum(&L); print(&L);// Print to see if the deletion is successful
return 0; }

Technology
Daily Recommendation
views 1