프로그래머스 C# 중복된 숫자 개수(foreach 파이썬, 자바)

2023. 4. 2. 10:58C# 알고리즘 코딩(프로그래머스)

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;
    }
}