C Program to Remove Duplicate Elements From a Sorted Array

C Program to Remove Duplicate Elements From a Sorted Array

A class stores five marks in their original order: 40, 35, 45, 35 and 50. The remove sorted duplicates task needs a clear rule about order, duplicates and how many elements exist.

For a sorted nonempty array, retain a value only when it differs from the last retained value; return or track the new logical length.

Before this exercise, review array elements and bounds and function parameters. The worked program below applies those ideas to this specific problem.

  1. Explain

    Explain why remove sorted duplicates uses this sequence of operations.

  2. Trace

    Trace the sample values and predict the printed result.

  3. Apply

    Remove duplicates from the unsorted original while preserving first-occurrence order, using a small explicit seen-value scan.

Remove Sorted Duplicates: follow the data

  1. Given data

    A class stores five marks in their original order: 40, 35, 45, 35 and 50. The remove sorted duplicates task needs a clear rule about order, duplicates and how many elements exist.

  2. Rule

    For a sorted nonempty array, retain a value only when it differs from the last retained value; return or track the new logical length.

  3. Observed result

    35 40 45 50

A guided model of this example. The illustration explains the process; run the complete C program to check its output.

Understand the operation

Sorting makes equal values adjacent. read visits the input, while used identifies the next output position. The allocation size stays the same even when fewer elements are logically retained.

The complete example knows its five-element bound. In a reusable function, pass an explicit element count with the array pointer; a pointer alone does not report that bound. This sample has two equal marks, which makes duplicate handling observable.

Read the complete C program

The input values are written into this complete C17 example so the calculation can be reproduced without an unavailable service. Change the named data, rebuild and compare the actual output with your prediction.

C17 · complete program

#include <stdio.h>
int main(void) {
  int marks[] = {40, 35, 45, 35, 50};
  int count = (int)(sizeof marks / sizeof marks[0]);
  int ordered[] = {35, 35, 40, 45, 50};
  int used = 1;
  for (int read = 1; read < count; read++) {
    if (ordered[read] != ordered[used - 1]) ordered[used++] = ordered[read];
  }
  for (int i = 0; i < used; i++) printf("%d%s", ordered[i], i == used - 1 ? "\n" : " ");
  return 0;
}

Expected output

35 40 45 50
Run in C compiler →

Follow the values through the code

C17 · code used in this explanation

#include <stdio.h>
int main(void) {
  int marks[] = {40, 35, 45, 35, 50};
  int count = (int)(sizeof marks / sizeof marks[0]);
  int ordered[] = {35, 35, 40, 45, 50};
  int used = 1;
  for (int read = 1; read < count; read++) {
    if (ordered[read] != ordered[used - 1]) ordered[used++] = ordered[read];
  }
  for (int i = 0; i < used; i++) printf("%d%s", ordered[i], i == used - 1 ? "\n" : " ");
  return 0;
}

The second 35 matches the retained 35 and is skipped; 40,45 and 50 each extend the retained prefix.

Compare the resulting output with the original sequence. A changed order and a changed set of values are different effects; decide which your operation promises.

Follow the worked calculation

  1. Start with the sample

    The second 35 matches the retained 35 and is skipped; 40,45 and 50 each extend the retained prefix.

  2. Apply the rule

    Compare the resulting output with the original sequence. A changed order and a changed set of values are different effects; decide which your operation promises.

  3. Check the result

    35 40 45 50

    35 40 45 50
A guided model of this example. The illustration explains the process; run the complete C program to check its output.

Try a small variation

C17 · code used in this explanation

#include <stdio.h>
int main(void) {
  int marks[] = {40, 35, 45, 35, 50};
  int count = (int)(sizeof marks / sizeof marks[0]);
  int ordered[] = {35, 35, 40, 45, 50};
  int used = 1;
  for (int read = 1; read < count; read++) {
    if (ordered[read] != ordered[used - 1]) ordered[used++] = ordered[read];
  }
  for (int i = 0; i < used; i++) printf("%d%s", ordered[i], i == used - 1 ? "\n" : " ");
  return 0;
}

Remove duplicates from the unsorted original while preserving first-occurrence order, using a small explicit seen-value scan.

Worked answer

The complete answer keeps the five valid input elements and applies the stated alternative. Read the output against the original array rather than assume that every loop should sort, remove or overwrite values.

C17 · complete program

#include <stdio.h>
int main(void) {
  int marks[] = {40, 35, 45, 35, 50};
  int count = (int)(sizeof marks / sizeof marks[0]);
  int used = 0, unique[5];
  for (int read = 0; read < count; read++) {
    int seen = 0;
    for (int i = 0; i < used; i++) if (unique[i] == marks[read]) seen = 1;
    if (!seen) unique[used++] = marks[read];
  }
  for (int i = 0; i < used; i++) printf("%d%s", unique[i], i == used - 1 ? "\n" : " ");
  return 0;
}

Expected output

40 35 45 50
Run in C compiler →

Recognize the important boundaries

Starting used at one requires a nonempty input. Values beyond the new logical length are not part of the deduplicated result.

The sample marks and their sums fit int. General input requires both valid array bounds and an arithmetic-range contract.

Check your understanding

Choose the answer, then compare the reasoning for every option.

Why is count passed separately to a reusable array function?
Choose one answer. You can try again.

Answer and explanation

The element pointer does not carry the caller’s element count.

Correct. The separate bound defines which accesses are permitted.

Use the idea independently

For a sorted nonempty array, retain a value only when it differs from the last retained value; return or track the new logical length.

Continue with array indexes and bounds.

Continue with pointer and count contracts.

Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: C Program to Merge Two Arrays →