๐Ÿ‘ฉ‍๐Ÿ’ปProgramming/Coding Test

[C#][Programmers] ๋ฐฐ์—ด์˜ ์œ ์‚ฌ๋„

taesooya 2022. 11. 26.

๋‘ ๋ฐฐ์—ด์ด ์–ผ๋งˆ๋‚˜ ์œ ์‚ฌํ•œ์ง€ ํ™•์ธํ•ด๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด ๋ฐฐ์—ด s1๊ณผ s2๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ ๊ฐ™์€ ์›์†Œ์˜ ๊ฐœ์ˆ˜๋ฅผ returnํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

 

for๋ฌธ ์‚ฌ์šฉ์‹œ

using System;

public class Solution {
    public int solution(string[] s1, string[] s2) {
        int answer = 0;
        for(int i = 0; i<s1.Length; i++)
        {
            for(int j = 0; j<s2.Length; j++)
            {
                if(s1[i].Equals(s2[j]))
                    answer++;
            }
        }
        return answer;
    }
}

foreach๋ฌธ ์‚ฌ์šฉ์‹œ

using System;

public class Solution {
    public int solution(string[] s1, string[] s2) {
        int answer = 0;
        foreach (string single_str1 in s1)
            foreach (string single_str2 in s2)
                if (single_str1.Equals(single_str2))
                    answer++;
        return answer;
    }
}

Linq.Count ์‚ฌ์šฉ์‹œ

using System;
using System.Linq;

public class Solution {
    public int solution(string[] s1, string[] s2) {
        return s1.Count(x => s2.Contains(x));
    }
}


Contains์˜ ๋‹ค๋ฅธ ๊ฒฐ๊ณผ๊ฐ’

Linq๋Š” ๋™์ผํ•  ๊ฒฝ์šฐ

๋Œ“๊ธ€