Essential Array Manipulation Programs in C: Removing Duplicates, Rotating, and More

In this lecture, I have explained more 10 programming on arrays in C programming. In your previous class, you learned about the basic features of the array with some basic programming. In this article, I am going to explain more complex array programs.

1. Program to Remove Duplicates from an Array

#include <stdio.h>

int main() {

    int arr[10], n, i, j, k;

    printf(“Enter the number of elements: “);

    scanf(“%d”, &n);

    printf(“Enter %d integers: “, n);

    for (i = 0; i < n; i++) {

        scanf(“%d”, &arr[i]);

    }

    for (i = 0; i < n; i++) {

        for (j = i + 1; j < n; j++) {

            if (arr[i] == arr[j]) {

                for (k = j; k < n – 1; k++) {

                    arr[k] = arr[k + 1];

                }

                n–;

                j–;

            }

        }

    }

    printf(“Array after removing duplicates: “);

    for (i = 0; i < n; i++) {

        printf(“%d “, arr[i]);

    }

    return 0;

}

DRY-RUN:

Stepi (Outer Loop)j (Inner Loop)arr[]Action Taken
101[1, 2, 2, 3, 4, 4, 5]Compare arr[0] with arr[1], no change.
202[1, 2, 2, 3, 4, 4, 5]Compare arr[0] with arr[2], remove duplicate 2.
303[1, 2, 3, 4, 4, 5]Compare arr[0] with arr[3], no change.
404[1, 2, 3, 4, 4, 5]Compare arr[0] with arr[4], remove duplicate 4.
505[1, 2, 3, 4, 5]Compare arr[0] with arr[5], no change.
612[1, 2, 3, 4, 5]Compare arr[1] with arr[2], no change.
713[1, 2, 3, 4, 5]Compare arr[1] with arr[3], no change.
814[1, 2, 3, 4, 5]Compare arr[1] with arr[4], no change.
915[1, 2, 3, 4, 5]Compare arr[1] with arr[5], no change.
1023[1, 2, 3, 4, 5]Compare arr[2] with arr[3], no change.
1124[1, 2, 3, 4, 5]Compare arr[2] with arr[4], no change.
1225[1, 2, 3, 4, 5]Compare arr[2] with arr[5], no change.
1334[1, 2, 3, 4, 5]Compare arr[3] with arr[4], no change.
1435[1, 2, 3, 4, 5]Compare arr[3] with arr[5], no change.
1545[1, 2, 3, 4, 5]Compare arr[4] with arr[5], no change.
16EndEnd[1, 2, 3, 4, 5]Final array after removing duplicates.
WhatsApp Group Join Now
Telegram Group Join Now

2. Program to Rotate an Array to the Right

#include <stdio.h>

int main() {

    int arr[5], i, j, temp;

    printf(“Enter 5 integers: “);

    for (i = 0; i < 5; i++) {

        scanf(“%d”, &arr[i]);

    }

    temp = arr[4];

    for (i = 4; i > 0; i–) {

        arr[i] = arr[i – 1];

    }

    arr[0] = temp;

    printf(“Array after right rotation: “);

    for (i = 0; i < 5; i++) {

        printf(“%d “, arr[i]);

    }

    return 0;

}

DRY-RUN:

Stepiarr[]tempAction Taken
10[1, 2, 3, 4, 5]Input: arr[0] = 1, arr[1] = 2, arr[2] = 3, arr[3] = 4, arr[4] = 5
21[1, 2, 3, 4, 5]Loop input complete, last element (arr[4]) is stored in temp = 5
32[1, 2, 3, 4, 5]5Shift arr[4] to arr[3]: arr[4] = arr[3], arr[] becomes [1, 2, 3, 4, 4]
43[1, 2, 3, 4, 4]5Shift arr[3] to arr[2]: arr[3] = arr[2], arr[] becomes [1, 2, 3, 3, 4]
54[1, 2, 3, 3, 4]5Shift arr[2] to arr[1]: arr[2] = arr[1], arr[] becomes [1, 2, 2, 3, 4]
65[1, 2, 2, 3, 4]5Shift arr[1] to arr[0]: arr[1] = arr[0], arr[] becomes [1, 1, 2, 3, 4]
76[5, 2, 2, 3, 4]5arr[0] is set to temp = 5, final array becomes [5, 1, 2, 3, 4]
8End[5, 1, 2, 3, 4]The array after rotation is printed. Output: 5 1 2 3 4

3. Program to Count Occurrences of a Specific Element in an Array

#include <stdio.h>

int main() {

    int arr[5], target, count = 0;

    printf(“Enter 5 integers: “);

    for (int i = 0; i < 5; i++) {

        scanf(“%d”, &arr[i]);

    }

    printf(“Enter the number to count: “);

    scanf(“%d”, &target);

    for (int i = 0; i < 5; i++) {

        if (arr[i] == target) {

            count++;

        }

    }

    printf(“The number %d appears %d times.”, target, count);

    return 0;

}

DRY-RUN:

Stepiarr[]targetcountAction Taken
10[1, 2, 3, 2, 4]20Input: arr[0] = 1, arr[1] = 2, arr[2] = 3, arr[3] = 2, arr[4] = 4
21[1, 2, 3, 2, 4]20arr[0] = 1 is not equal to target = 2, so count remains 0.
32[1, 2, 3, 2, 4]20arr[1] = 2 is equal to target = 2, so increment count to 1.
43[1, 2, 3, 2, 4]21arr[2] = 3 is not equal to target = 2, so count remains 1.
54[1, 2, 3, 2, 4]21arr[3] = 2 is equal to target = 2, so increment count to 2.
65[1, 2, 3, 2, 4]22arr[4] = 4 is not equal to target = 2, so count remains 2.
7End[1, 2, 3, 2, 4]22The final count is printed: “The number 2 appears 2 times.”

4. Program to Find the Second Largest Element in an Array

#include <stdio.h>

int main() {

    int arr[5], largest, second_largest;

    printf(“Enter 5 integers: “);

    for (int i = 0; i < 5; i++) {

        scanf(“%d”, &arr[i]);

    }

    largest = second_largest = arr[0];

    for (int i = 1; i < 5; i++) {

        if (arr[i] > largest) {

            second_largest = largest;

            largest = arr[i];

        } else if (arr[i] > second_largest && arr[i] != largest) {

            second_largest = arr[i];

        }

    }

    printf(“Second largest element: %d”, second_largest);

    return 0;

}

DRY-RUN:

Stepiarr[]largestsecond_largestAction Taken
10[10, 20, 4, 45, 30]1010Input: arr[0] = 10, arr[1] = 20, arr[2] = 4, arr[3] = 45, arr[4] = 30
21[10, 20, 4, 45, 30]1010arr[1] = 20 is greater than largest = 10, so update: largest = 20, second_largest = 10.
32[10, 20, 4, 45, 30]2010arr[2] = 4 is smaller than both largest and second_largest, no change.
43[10, 20, 4, 45, 30]2010arr[3] = 45 is greater than largest = 20, so update: largest = 45, second_largest = 20.
54[10, 20, 4, 45, 30]4520arr[4] = 30 is smaller than both largest and second_largest, no change.
6End[10, 20, 4, 45, 30]4520The second largest element is 20, which is printed.

5. Program to Check If an Array is Palindrome

#include <stdio.h>

int main() {

    int arr[5], i, is_palindrome = 1;

    printf(“Enter 5 integers: “);

    for (i = 0; i < 5; i++) {

        scanf(“%d”, &arr[i]);

    }

    for (i = 0; i < 5 / 2; i++) {

        if (arr[i] != arr[5 – i – 1]) {

            is_palindrome = 0;

            break;

        }

    }

    if (is_palindrome) {

        printf(“The array is a palindrome.”);

    } else {

        printf(“The array is not a palindrome.”);

    }

    return 0;

}

DRY-RUN:

Stepiarr[]is_palindromeAction Taken
10[1, 2, 3, 4, 5]1Input: arr[0] = 1, arr[1] = 2, arr[2] = 3, arr[3] = 4, arr[4] = 5
20[1, 2, 3, 4, 5]1Check: arr[0] = 1, arr[4] = 5 → they don’t match, set is_palindrome = 0 and break.
3End[1, 2, 3, 4, 5]0The final output: “The array is not a palindrome.”

6. Program to Find the Frequency of Each Element in an Array

#include <stdio.h>

int main() {

    int arr[5], freq[5] = {0}, i, j;

    printf(“Enter 5 integers: “);

    for (i = 0; i < 5; i++) {

        scanf(“%d”, &arr[i]);

    }

    for (i = 0; i < 5; i++) {

        if (freq[i] == 0) {

            freq[i] = 1;

            for (j = i + 1; j < 5; j++) {

                if (arr[i] == arr[j]) {

                    freq[i]++;

                }

            }

        }

    }

    printf(“Element frequencies: “);

    for (i = 0; i < 5; i++) {

        if (freq[i] > 0) {

            printf(“%d occurs %d times\n”, arr[i], freq[i]);

        }

    }

    return 0;

}

DRY-RUN:

Stepiarr[]freq[]jAction Taken
10[1, 2, 2, 3, 1][0, 0, 0, 0, 0]Input the array values: arr[0] = 1, arr[1] = 2, arr[2] = 2, arr[3] = 3, arr[4] = 1
20[1, 2, 2, 3, 1][1, 0, 0, 0, 0]1For arr[0] = 1, start checking. freq[0] becomes 1.
31[1, 2, 2, 3, 1][1, 1, 0, 0, 0]2For arr[1] = 2, start checking. freq[1] becomes 1.
41[1, 2, 2, 3, 1][1, 2, 0, 0, 0]3arr[1] = 2, arr[2] = 2 (match) → freq[1] increments to 2.
52[1, 2, 2, 3, 1][1, 2, 0, 0, 0]Continue, no match needed. freq[2] remains 0.
63[1, 2, 2, 3, 1][1, 2, 0, 1, 0]For arr[3] = 3, no further match needed. freq[3] becomes 1.
74[1, 2, 2, 3, 1][2, 2, 0, 1, 0]For arr[4] = 1, arr[0] = 1 (match) → freq[0] increments to 2.
8End[1, 2, 2, 3, 1][2, 2, 0, 1, 0]Final frequencies: 1 occurs 2 times, 2 occurs 2 times, 3 occurs 1 time.
9Final OutputOutput: “Element frequencies:1 occurs 2 times,2 occurs 2 times, 3 occurs 1 time.”

7. Program to Insert an Element at a Specific Position

#include <stdio.h>

int main() {

    int arr[6], i, pos, element;

    printf(“Enter 5 integers: “);

    for (i = 0; i < 5; i++) {

        scanf(“%d”, &arr[i]);

    }

    printf(“Enter position and element to insert: “);

    scanf(“%d %d”, &pos, &element);

    for (i = 5; i >= pos; i–) {

        arr[i] = arr[i – 1];

    }

    arr[pos – 1] = element;

    printf(“Array after insertion: “);

    for (i = 0; i < 6; i++) {

        printf(“%d “, arr[i]);

    }

    return 0;

}

DRY-RUN:

Stepiarr[]poselementAction Description
1[1, 2, 3, 4, 5]Input the original array: 1, 2, 3, 4, 5
20[1, 2, 3, 4, 5]399Input position = 3, element to insert = 99.
34[1, 2, 3, 4, 5]Start shifting elements starting from position 5: arr[5] = arr[4]. arr becomes [1, 2, 3, 4, 5, 5]
43[1, 2, 3, 4, 5, 5]arr[4] = arr[3]. arr becomes [1, 2, 3, 4, 4, 5]
52[1, 2, 3, 4, 4, 5]arr[3] = arr[2]. arr becomes [1, 2, 3, 3, 4, 5]
61[1, 2, 3, 3, 4, 5]arr[2] = arr[1]. arr becomes [1, 2, 2, 3, 4, 5]
70[1, 2, 2, 3, 4, 5]arr[1] = arr[0]. arr becomes [1, 1, 2, 3, 4, 5]
8[1, 2, 99, 3, 4, 5]Insert element 99 at position 3: arr[2] = 99
9Final[1, 2, 99, 3, 4, 5]Output: “Array after insertion: 1 2 99 3 4 5”

8. Program to Delete an Element from an Array

#include <stdio.h>

int main() {

    int arr[5], i, pos;

    printf(“Enter 5 integers: “);

    for (i = 0; i < 5; i++) {

        scanf(“%d”, &arr[i]);

    }

    printf(“Enter the position to delete: “);

    scanf(“%d”, &pos);

    for (i = pos – 1; i < 4; i++) {

        arr[i] = arr[i + 1];

    }

    printf(“Array after deletion: “);

    for (i = 0; i < 4; i++) {

        printf(“%d “, arr[i]);

    }

    return 0;

}

DRY-RUN:

Stepiarr[]posAction Description
1[1, 2, 3, 4, 5]Input the original array: 1, 2, 3, 4, 5
23Input position to delete: position = 3
32[1, 2, 3, 4, 5]Start shifting elements starting from position 3: arr[2] = arr[3]. arr becomes [1, 2, 4, 4, 5]
43[1, 2, 4, 4, 5]arr[3] = arr[4]. arr becomes [1, 2, 4, 5, 5]
5Final[1, 2, 4, 5, 5]Last element (5) is effectively removed as array shrinks to 4 elements.
6Final[1, 2, 4, 5]Output: “Array after deletion: 1 2 4 5”

9. Program to Check If Two Arrays Are Equal

#include <stdio.h>

int main() {

    int arr1[5], arr2[5], equal = 1;

    printf(“Enter 5 integers for first array: “);

    for (int i = 0; i < 5; i++) {

        scanf(“%d”, &arr1[i]);

    }

    printf(“Enter 5 integers for second array: “);

    for (int i = 0; i < 5; i++) {

        scanf(“%d”, &arr2[i]);

    }

    for (int i = 0; i < 5; i++) {

        if (arr1[i] != arr2[i]) {

            equal = 0;

            break;

        }

    }

    if (equal) {

        printf(“Arrays are equal.”);

    } else {

        printf(“Arrays are not equal.”);

    }

    return 0;

}

DRY-RUN:

Stepiarr1[]arr2[]equalAction Description
1[1, 2, 3, 4, 5]Input first array: 1 2 3 4 5
2[1, 2, 3, 4, 6]Input second array: 1 2 3 4 6
30[1, 2, 3, 4, 5][1, 2, 3, 4, 6]1Compare arr1[0] = arr2[0] (1 == 1), continue loop
41[1, 2, 3, 4, 5][1, 2, 3, 4, 6]1Compare arr1[1] = arr2[1] (2 == 2), continue loop
52[1, 2, 3, 4, 5][1, 2, 3, 4, 6]1Compare arr1[2] = arr2[2] (3 == 3), continue loop
63[1, 2, 3, 4, 5][1, 2, 3, 4, 6]1Compare arr1[3] = arr2[3] (4 == 4), continue loop
74[1, 2, 3, 4, 5][1, 2, 3, 4, 6]0Compare arr1[4] = arr2[4] (5 != 6), set equal to 0 and break loop
80Print “Arrays are not equal.”

10. Program to Sum Two Arrays Element-Wise

#include <stdio.h>

int main() {

    int arr1[5], arr2[5], sum[5];

    printf(“Enter 5 integers for first array: “);

    for (int i = 0; i < 5; i++) {

        scanf(“%d”, &arr1[i]);

    }

    printf(“Enter 5 integers for second array: “);

    for (int i = 0; i < 5; i++) {

        scanf(“%d”, &arr2[i]);

    }

    for (int i = 0; i < 5; i++) {

        sum[i] = arr1[i] + arr2[i];

    }

    printf(“Sum of two arrays: “);

    for (int i = 0; i < 5; i++) {

        printf(“%d “, sum[i]);

    }

    return 0;

}

DRY-RUN:

Stepiarr1[]arr2[]sum[]Action Description
1[1, 2, 3, 4, 5][5, 4, 3, 2, 1][0, 0, 0, 0, 0]Input both arrays
20[1, 2, 3, 4, 5][5, 4, 3, 2, 1][6, 0, 0, 0, 0]sum[0] = 1 + 5 = 6
31[1, 2, 3, 4, 5][5, 4, 3, 2, 1][6, 6, 0, 0, 0]sum[1] = 2 + 4 = 6
42[1, 2, 3, 4, 5][5, 4, 3, 2, 1][6, 6, 6, 0, 0]sum[2] = 3 + 3 = 6
53[1, 2, 3, 4, 5][5, 4, 3, 2, 1][6, 6, 6, 6, 0]sum[3] = 4 + 2 = 6
64[1, 2, 3, 4, 5][5, 4, 3, 2, 1][6, 6, 6, 6, 6]sum[4] = 5 + 1 = 6
7[6, 6, 6, 6, 6]Output: “Sum of two arrays: 6 6 6 6 6”

Leave a Comment