๐Ÿ‘ฉ‍๐Ÿ’ปProgramming/C#

[C#]Linq - Distinct

taesooya 2022. 12. 25.

 

https://learn.microsoft.com/ko-kr/dotnet/api/system.linq.enumerable.distinct?view=net-7.0 

 

Enumerable.Distinct ๋ฉ”์„œ๋“œ (System.Linq)

์‹œํ€€์Šค์˜ ๊ณ ์œ  ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

learn.microsoft.com

๊ธฐ๋ณธ ๊ฐ™์Œ ๋น„๊ต์ž๋กœ ๊ฐ’์„ ๋น„๊ตํ•˜์—ฌ ์‹œํ€€์Šค์—์„œ ๊ณ ์œ  ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

 

 

์˜ˆ์ œ

List<int> ages = new List<int> { 21, 46, 46, 55, 17, 21, 55, 55 };

IEnumerable<int> distinctAges = ages.Distinct();

Console.WriteLine("Distinct ages:");

foreach (int age in distinctAges)
{
    Console.WriteLine(age);
}

/*
 This code produces the following output:

 Distinct ages:
 21
 46
 55
 17
*/
// ๋ฌธ์ž์—ด my_string์ด ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. 
// my_string์—์„œ ์ค‘๋ณต๋œ ๋ฌธ์ž๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  ํ•˜๋‚˜์˜ ๋ฌธ์ž๋งŒ ๋‚จ๊ธด ๋ฌธ์ž์—ด์„ returnํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

using System;
using System.Linq;

public class Solution {
    public string solution(string my_string) {
        return String.Join("", my_string.Distinct());
    }
}

'๐Ÿ‘ฉโ€๐Ÿ’ปProgramming > C#' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[C#] LINQ  (0) 2022.12.21
[C#] Array ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”์„œ๋“œ  (0) 2022.12.19
[C#] Char.IsNumber / IsDigit  (0) 2022.12.11
[C#] Math ๋ฉ”์„œ๋“œ  (0) 2022.11.26
[C#] out  (0) 2022.07.31

๋Œ“๊ธ€