Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

i want a code in c language that recieve some numbers and it must count the Repetitious…

  1. User

    i want a code in c language that recieve some numbers and it must count the Repetitious دعئذثقس

  2. Assistant

    Sure, here's an example code in C language that prompts the user to enter some numbers and then counts the repetitive elements:

    #include <stdio.h>
    
    #define MAX_SIZE 100
    
    int main() {
        int array[MAX_SIZE];
        int count[MAX_SIZE];
        int size, i, j, duplicateCount;
    
        printf("Enter the size of the array: ");
        scanf("%d", &size);
    
        printf("Enter the elements of the array:\n");
        for (i = 0; i < size; i++) {
            scanf("%d", &array[i]);
            count[i] = -1;
        }
    
        duplicateCount = 0;
        for (i = 0; i < size; i++) {
            int occurrence = 1;
            for (j = i + 1; j < size; j++) {
                if (array[i] == array[j]) {
                    occurrence++;
    
                    // Set count of jth element to 0
                    count[j] = 0;
                }
            }
    
            // If count of current element is not 0
            if (count[i] != 0) {
                count[i] = occurrence;
                duplicateCount++;
            }
        }
    
        printf("\nDuplicate elements in the array are:\n");
        for (i = 0; i < size; i++) {
            if (count[i] > 1) {
                printf("%d occurs %d times\n", array[i], count[i]);
            }
        }
    
        printf("Total count of duplicate elements: %d\n", duplicateCount);
    
        return 0;
    }
    

    In this code, the array count[] is used to keep track of the count of occurrences of each element in the array[]. It first initializes all elements of count[] to -1, and during the iteration, whenever it finds a duplicate element, it sets the count of that element to 0 to mark it.

    Then it checks if the count of current element is not 0, it means it occurs more than once and it increments the duplicateCount variable.

    Finally, it prints the duplicate elements along with their counts, and the total count of duplicate elements.

    Note: This code assumes that the input size is less than or equal to MAX_SIZE.