Distinct Numbers

You are given a list of n integers, and your task is to calculate the number of distinct values in the list.

Example:

Input: n = 5, arr = {2, 3, 2, 2, 3}
Output: 2

Approach

C++

#include <bits/stdc++.h>
using namespace std;

void distinctNumbers(int n, vector<int> &arr)
{
    set<int> st;
    for (int i = 0; i < n; i++)
        st.insert(arr[i]);

    cout << st.size() << "\n";
}
int main()
{
    int n = 5;
    vector<int> arr = {2, 3, 2, 2, 3};

    distinctNumbers(n, arr);

    return 0;
}


No comments:

Post a Comment