- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Sets in C#
Sets in C# is a HashSet. HashSet in C# eliminates duplicate strings or elements in an array. In C#, it is an optimized set collection
To declare HashSet −
var h = new HashSet<string>(arr1);
Above, we have set the already declared array arr1 in the HashSet.
Now set it on the array to remove the duplicate words −
string[] arr2 = h.ToArray();
Let us see an example to remove duplicate strings using C# HashSet.
Here, we have duplicate elements −
Example
using System; using System.Collections.Generic; using System.Linq; class Program { static void Main() { string[] arr1 = {"Table","Chair","Pen","Clip","Table"}; Console.WriteLine(string.Join(",", arr1)); // HashSet var h = new HashSet<string>(arr1); // eliminates duplicate words string[] arr2 = h.ToArray(); Console.WriteLine(string.Join(",", arr2)); } }
Advertisements