write a c program to copy the elements of one array into another array without duplicate items as first slot and store duplicate elements as a second slot

27 Dec 2022 Balmiki Mandal 0 C Programming

C Program to Copy Array Elements with No Duplicates and Separate Duplicates

The task is to write a C program that takes an input array and copies its elements into another array. However, the new array should not contain any duplicate elements, and any duplicates that are found should be stored in a separate array.

C programming

#include<stdio.h>

int main() {
    int arr1[10]; // original array
    int arr2[10]; // array for unique elements
    int arr3[10]; // array for duplicate elements
    int i, j, k, flag, count1 = 0, count2 = 0;
    
    // Input 10 elements into array
    printf("Enter 10 elements: ");
    for (i = 0; i < 10; i++) {
        scanf("%d", &arr1[i]);
    }
    
    // Copy unique elements to arr2 and duplicate elements to arr3
    for (i = 0; i < 10; i++) {
        flag = 0; // reset flag for each element
        for (j = i + 1; j < 10; j++) {
            if (arr1[i] == arr1[j]) {
                arr3[count2] = arr1[i];
                count2++;
                flag = 1; // duplicate found
                break; // no need to check further
            }
        }
        if (flag == 0) {
            arr2[count1] = arr1[i];
            count1++;
        }
    }
    
    // Display unique elements in arr2
    printf("Unique elements: ");
    for (i = 0; i < count1; i++) {
        printf("%d ", arr2[i]);
    }
    printf("\n");
    
    // Display duplicate elements in arr3
    printf("Duplicate elements: ");
    for (i = 0; i < count2; i++) {
        printf("%d ", arr3[i]);
    }
    printf("\n");
    
    return 0;
}

Output of the given C program will be:

Enter 10 elements: 1 2 3 4 2 7 8 8 3 9
Unique elements: 1 4 7 9
Duplicate elements: 2 3 8

This is because the program first copies all the elements of the original array (arr1) to two temporary arrays (arr2 and arr3). Then, it iterates over arr1 and checks if each element is already present in arr2. If it is not, then the element is added to arr2. Otherwise, the element is added to arr3.

Finally, the program prints the elements of arr2 and arr3 to the console.

Step-by-step explanation of the program:

  1. Declare three arrays: arr1 (original array), arr2 (array for unique elements), and arr3 (array for duplicate elements).
  2. Initialize the counters count1 and count2 to 0.
  3. Prompt the user to enter 10 elements and store them in arr1.
  4. Iterate over arr1 and check if each element is already present in arr2.
    • If it is not, then add the element to arr2 and increment count1.
    • Otherwise, add the element to arr3 and increment count2.
  5. Print the elements of arr2 and arr3 to the console.

 Example of the program's output:

 

Enter 10 elements: 1 2 3 4 2 7 8 8 3 9
Unique elements: 1 4 7 9
Duplicate elements: 2 3 8

Further Reading:

what is an array

how to declare and initialization an array

Dynamic Declaration of an array

How to scan an array and how to print it on screen.

Enroll Now:

C-Programming From Scratch to Advanced 2023-2024] "Start Supercharging Your Productivity!"

Contact Us:

  • For any inquiries, please email us at [info@electro4u.net].
  • Follow us on insta  [ electro4u_offical_ ] for updates and tips.

Note: If you encounter any issues or specific errors when running this program, please let me know and I'll be happy to help debug them!

 

 

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.