Post

[Programmers] #120583 - 중복된 숫자 개수 [Java][C++][Python]

[Programmers] #120583 - 중복된 숫자 개수 [Java][C++][Python]

문제 링크


1. 아이디어

배열에서 $n$과 같은 원소의 개수를 세는 문제로, 원소마다 $n$과 같은지만 확인해 세면 된다.


2. 코드

풀이 [Java][C++][Python]

1
2
3
4
5
6
7
8
9
10
class Solution {
    public int solution(int[] array, int n) {
        int cnt = 0;
        for (int x : array) {
            if (x == n) cnt++;
        }

        return cnt;
    }
}
1
2
3
4
5
6
#include <bits/stdc++.h>
using namespace std;

int solution(vector<int> array, int n) {
    return count(array.begin(), array.end(), n);
}
1
2
def solution(array, n):
    return array.count(n)

This post is licensed under CC BY 4.0 by the author.