Menu
Coddy logo textTech

Practice #3

Lesson 12 of 14 in Coddy's Linked List - Data Structures Series #5 course.

challenge icon

Challenge

Easy

Given a sorted integer array arr (the values of a sorted linked list), write a function removeDuplicates that returns the array with consecutive duplicates removed.

Because the input is sorted, equal values sit next to each other. Keep the first occurrence of each value and skip the rest.

Use a linked list to solve this problem!

You must use the LinkedList class (provided in linkedlist.<ext> along with node.<ext>) — do not use language built-ins like arrays' built-in reverse, slicing, or stdlib list operations to compute the result.

Try it yourself

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "solution.h"

int main() {
    char line[8192];
    if (!fgets(line, sizeof(line), stdin)) line[0] = '\0';
    int arr[4096];
    int len = 0;
    char* tok = strtok(line, " \t\r\n");
    while (tok) { arr[len++] = atoi(tok); tok = strtok(NULL, " \t\r\n"); }
    int rs = 0;
    int* r = removeDuplicates(arr, len, &rs);
    for (int idx = 0; idx < rs; idx++) {
        if (idx > 0) printf(" ");
        printf("%d", r[idx]);
    }
    printf("\n");
    return 0;
}

All lessons in Linked List - Data Structures Series #5