Logo

Programming-Idioms

History of Idiom 118 > diff from v59 to v60

Edit summary for version 60 by domn1995:
[C#] Making it more comparable and using new language features that simplify the code.

Version 59

2021-03-31, 20:16:26

Version 60

2021-10-01, 21:46:36

Idiom #118 List to set

Create set y from list x.
x may contain duplicates. y is unordered and has no repeated values.

Idiom #118 List to set

Create set y from list x.
x may contain duplicates. y is unordered and has no repeated values.

Turning the list [a,b,c,b] into the set {c,a,b}
Variables
y,x
Variables
y,x
Imports
using System.Collections.Generic;
Imports
using System.Collections.Generic;
Code
static void Main(string[] args)
{
    string[] x = new string[] { "a", "b", "c", "b" };
    HashSet<string> y = new HashSet<string>(x);
}
Code
var x = new[] { "a", "b", "c", "b" };
var y = new HashSet<string>(x);

Comments bubble
Relies on the default equality comparer for the set type. Can be used for any type that implements such an equality comparer.
Comments bubble
Relies on the default equality comparer for the set type. Can be used for any type that implements such an equality comparer.
Doc URL
https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.hashset-1.-ctor?view=netframework-4.8#System_Collections_Generic_HashSet_1__ctor_System_Collections_Generic_IEnumerable__0__
Doc URL
https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.hashset-1.-ctor?view=netframework-4.8#System_Collections_Generic_HashSet_1__ctor_System_Collections_Generic_IEnumerable__0__