How to create a set with only the unique values in C#

1 Answer

0 votes
using System;
using System.Collections.Generic;

class Program
{
    static void Main() {
        HashSet<int> hashset = new HashSet<int>();

        hashset.Add(1);
        hashset.Add(2);
        hashset.Add(3);
        hashset.Add(4); 
        hashset.Add(1); 
        hashset.Add(1); 
        hashset.Add(2); 
        
        foreach (var item in hashset) {
           Console.Write(item + " ");
        }
    }
}




/*
run:

1 2 3 4 

*/

 



answered Aug 22, 2023 by avibootz

Related questions

3 answers 361 views
1 answer 266 views
1 answer 230 views
1 answer 230 views
1 answer 268 views
2 answers 345 views
1 answer 217 views
...