Skip to content

Commit b22e89a

Browse files
authored
Create 13_Distinct_Numbers.cpp
1 parent 8b837f0 commit b22e89a

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

C++/13_Distinct_Numbers.cpp

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <bits/stdc++.h>
2+
#define ll long long
3+
using namespace std;
4+
5+
int solution()
6+
{
7+
8+
int n, count=0;
9+
10+
//Taking input for number of elements in the array
11+
cin>>n;
12+
13+
//Declaring an array of size n
14+
int a[n];
15+
16+
//Taking n inputs for n no. of elements of the array
17+
for(int i=0; i<n;i++)
18+
{
19+
cin>>a[i];
20+
}
21+
22+
//Declaring a set which only stores distinct elements
23+
unordered_set<int> s;
24+
25+
//Inserting element inside set only if element is already no present and storing its count
26+
for(int i=0; i<n;i++)
27+
{
28+
if(s.find(a[i])==s.end())
29+
{
30+
s.insert(a[i]);
31+
count++;
32+
}
33+
}
34+
35+
//Printing number of distinct elements present
36+
cout << count;
37+
return 0;
38+
}
39+
40+
41+
int main()
42+
{
43+
44+
//Function call
45+
solution();
46+
return 0;
47+
}

0 commit comments

Comments
 (0)