[Programmers] #120583 - 중복된 숫자 개수 [Java][C++][Python]
[Programmers] #120583 - 중복된 숫자 개수 [Java][C++][Python]
1. 아이디어
array를 순회하며 각 원소가 n과 일치하면 카운팅하면 된다.
2. 복잡도
| 접근 | 시간 | 공간 |
|---|---|---|
| 풀이 | $O(N)$ | $O(1)$ |
($N$ = array의 길이)
3. 코드
풀이 [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.