Program that removes duplicates from an array while maintaining insertion order

C program to remove duplicates from an integer array while preserving insertion order using a simple O(n^2) algorithm for clarity and ease of use.

Brief Introduction

Program that removes duplicates from an array while maintaining insertion order (c)
DESCRIPTION:
This C program takes an array of integers, removes duplicate values while maintaining the original order of elements, and prints the resulting array without duplicates.

ADDITIONAL NOTES:
The program is designed to be simple and easily adaptable. Duplicate removal is done with an O(n^2) algorithm suitable for arrays of moderate size. For very large arrays, more efficient data structures like hash sets could be considered, but here clarity and simplicity are prioritized.

EXPLANATIONS:
- The removeDuplicates function iterates over each element of the input array and checks if it is already present in the output array.
- If the element has not yet been inserted, it is added to the output array.
- The order of elements is maintained because only the first encountered elements are added.
- The main function demonstrates the use of the function with a sample array and prints the result.
- Comments in the code are in Italian to facilitate understanding.

Example Code: c

#include <stdio.h>

// Function that removes duplicates from an array while maintaining the original order
// input: original array, its size
// output: array without duplicates, pointer to the size of the resulting array
void removeDuplicates(int input[], int inputSize, int output[], int *outputSize) {
    int i, j;
    *outputSize = 0; // initialize the size of the output array

    for (i = 0; i < inputSize; i++) {
        int trovato = 0; // flag to indicate if the element is already present

        // check if input[i] is already present in output
        for (j = 0; j < *outputSize; j++) {
            if (input[i] == output[j]) {
                trovato = 1; // duplicate found
                break;
            }
        }

        // if not found, add the element to the output array
        if (!trovato) {
            output[*outputSize] = input[i];
            (*outputSize)++;
        }
    }
}

int main() {
    // Sample array with duplicates
    int arrayOriginale[] = {4, 5, 9, 4, 5, 7, 9, 1};
    int dimensioneOriginale = sizeof(arrayOriginale) / sizeof(arrayOriginale[0]);

    // Array to hold values without duplicates
    int arraySenzaDuplicati[dimensioneOriginale];
    int dimensioneSenzaDuplicati = 0;

    // Remove duplicates
    removeDuplicates(arrayOriginale, dimensioneOriginale, arraySenzaDuplicati, &dimensioneSenzaDuplicati);

    // Print the result
    printf("Original array: ");
    for (int i = 0; i < dimensioneOriginale; i++) {
        printf("%d ", arrayOriginale[i]);
    }
    printf("\n");

    printf("Array without duplicates: ");
    for (int i = 0; i < dimensioneSenzaDuplicati; i++) {
        printf("%d ", arraySenzaDuplicati[i]);
    }
    printf("\n");

    return 0;
}
×
×
×
Do you want to regenerate the answer?
×
Export chat
Choose export format
Generazione PDF in corso…
Allegati
×
You are about to close the chat and switch to the image generator. If you are not logged in, you will lose our chat. Do you confirm?
You are viewing a shared chat in temporary mode. It will not be saved.
×
Saved prompts
×
Private note
×
Label
×
Search all chats
×
Your insights
Analyzing…
×
AI Preferences
Assistant name 30
Answer length
AI model
Structured answers
Include examples
Use emojis
Guide me, don't solve Hints and questions instead of a ready answer
More settings →
×
Share this chat
Anyone opening this link can view the chat or add it to their profile as their own chat.
Note: the chat attachments will be shared too. Anyone who adds it will receive a copy of the files in their own folder.
Shared chat
Someone shared a chat with you. Do you want to just view it or add it to your chats?
Note: the chat attachments will be shared too. Anyone who adds it will receive a copy of the files in their own folder.
×

Saved messages

Loading...

×

Chat History

example-programs · CHAT HISTORY

Loading...

AI Preferences

×
  • BasicQuick and essential answers for study
  • MediumHigher quality for study and programming
  • AdvancedComplex reasoning and detailed analysis
Explain Steps

What do you need?

Input integer array: contains the original values with possible duplicates.

Output integer array: contains the values without duplicates, maintaining order.

removeDuplicates function: implements the logic to remove duplicates.

main function: initializes the array, calls the duplicate removal function, and prints the result.

Development environment: standard C compiler (gcc, clang, etc.).

No external libraries required, uses only stdio.h for input/output.

Configuration: no particular hardware or software configuration needed.

Similar Topics

Sum of two integers entered from keyboard
C program that reads two integers from keyboard, calculates their sum, and prints the result using standard input output functions for beginners.
Conversion from Celsius to Fahrenheit
C program that converts Celsius to Fahrenheit by reading user input, validating it, and displaying the result with two decimal precision on the console.
Program that prints numbers from 1 to 1
This C program demonstrates printing numbers from 1 to 1 using a for loop, ideal for beginners learning basic C programming concepts.
Program that reads a character and displays its ASCII code
This C program reads a single character from the user and displays its ASCII code using getchar and printf functions in a simple beginner-friendly way.
Reading and Printing an Array of 5 Integers
C program that reads and prints an array of 5 integers using loops and arrays, ideal for beginners learning basic input output in C programming.
Search for a Number in a Small Array (Linear Search)
Simple C program demonstrating linear search in a small integer array to find a number and print the result for beginners.
Program that prints the greater of two numbers
C program that reads two integers from keyboard and prints the greater number using basic input output and conditional statements for beginners.
Program that prints the lowercase alphabet
This C program prints the lowercase alphabet from a to z using a for loop and char variables, demonstrating basic C programming concepts clearly and simply.