프로그래머스 C# 중복된 숫자 개수(foreach 파이썬, 자바)
2023. 4. 2. 10:58ㆍC# 알고리즘 코딩(프로그래머스)
using System;
public class Solution {
public int solution(int[] array, int n) {
int answer = 0;
for(int i = 0; i < array.Length; i++)
{
if(array[i] == n) // 어레이의 몇 번째에 n이랑 같은 게 있는 지 확인하기
{
answer++;
}
}
return answer;
}
}
/////
using System;
public class Solution {
public int solution(int[] array, int n) {
int answer = 0;
foreach (var it in array)
{
if (it == n)
{
answer++;
}
}
return answer;
}
}
//foreach로 꼭 it라고 하지 않아도 된다.
파이썬
def solution(array, n):
return array.count(n)
자바
class Solution {
public int solution(int[] array, int n) {
int answer = 0;
for (int num : array)
{
if (num == n) answer++;
}
return answer;
}
}
'C# 알고리즘 코딩(프로그래머스)' 카테고리의 다른 글
프로그래머스 C# 양꼬치(파이썬, 자바) (0) | 2023.04.05 |
---|---|
프로그래머스 C# 짝수의 합(파이썬, 자바) (0) | 2023.04.03 |
프로그래머스 C# 각도기(3항 연산자, 파이썬, 자바) (0) | 2023.04.02 |
프로그래머스 C# 숫자 비교하기(파이썬, 자바) (0) | 2023.03.31 |
프로그래머스 C# 나이 출력(파이썬) (0) | 2023.03.30 |