No title

11) Given a sequence of integers, find the number of distinct numbers in the sequence. The sequence need not be sorted. Input The input consists of two lines. The first line consists of a positive number N (N is at most 1000). The second line consists of N integers separated by spaces. Output The number of distinct elements in the sequence.

    
#include 
#include 
#define MAX_SIZE 1005

int distinct_numbers(int n, int arr[]) {
    int i, j, count = 0;
    int seen[MAX_SIZE] = {0};

    for (i = 0; i < n; i++) {
        if (seen[arr[i]] == 0) {
            count++;
            seen[arr[i]] = 1;
        }
    }
    return count;
}

int main() {
    int n, i;
    int arr[MAX_SIZE];

    scanf("%d", &n);
    for (i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }

    printf("%d\n", distinct_numbers(n, arr));
    return 0;
}
  
Previous Post Next Post