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 nvector<int&arr)
{
    set<intst;
    for (int i = 0i < ni++)
        st.insert(arr[i]);

    cout << st.size() << "\n";
}
int main()
{
    int n = 5;
    vector<intarr = {23223};

    distinctNumbers(narr);

    return 0;
}


No comments:

Post a Comment