프로그래밍
프로그래머스 C# 중복된 숫자 개수(array, foreach 간단한 사용법, P, J)
노마드선샤인
2022. 12. 20. 11:34
728x90
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;
}
}
728x90