알고리즘/CSES

[C++] [CSES] Distinct Numbers (Sorting and Searching)

조명인 2023. 2. 10. 23:12

CSES Sorting and Searching 첫번째 문제입니다.

배열에서 중복되지 않는 숫자의 개수를 출력하는 문제입니다.

set을 사용해서 간단하게 구하였습니다.

 

Task

 

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

Input

The first input line has an integer n: the number of values.

The second line has n integers x1,x2,,xn

Output

Print one integers: the number of distinct values.

Constraints

  • 1n21e5
  • 1xi1e9

Example

Input:
5
2 3 2 2 3

Output:
2

 

Code

#include<bits/stdc++.h>
using namespace std;
set<int> st;
int n, num;
int main(){
    ios::sync_with_stdio(0);cin.tie(0);
    cin >> n;
    while (n--){
        cin >> num;
        st.insert(num);
    }
    cout << st.size();
}

 생각해야 하는 것

  • 정렬한 후 for문 1회 돌면서 체크하는 방법도 있을 것 같습니다.