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 355 views
1 answer 260 views
1 answer 227 views
1 answer 225 views
1 answer 262 views
2 answers 341 views
1 answer 205 views
...