-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02_HashSet.cs
More file actions
102 lines (84 loc) · 6.11 KB
/
Copy path02_HashSet.cs
File metadata and controls
102 lines (84 loc) · 6.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using System;
using System.Collections.Generic;
namespace DataStructures
{
/// <summary>
/// Demonstrates the HashSet<T> collection in C#.
/// A HashSet stores unique elements and provides O(1) average-time
/// add, remove, and lookup operations. It supports mathematical set
/// operations such as union, intersection, and difference.
///
/// Common use cases: removing duplicates, membership testing,
/// set algebra (union, intersection, difference).
/// </summary>
public class HashSetExamples
{
// ─── Entry Point ───────────────────────────────────────────────
public static void Main()
{
Console.WriteLine("═══════════════════════════════════════════════");
Console.WriteLine(" C# HashSet<T> Examples ");
Console.WriteLine("═══════════════════════════════════════════════\n");
// ── 1. Basic Operations with Vowels ─────────────────────────
Console.WriteLine("── English Vowels ─────────────────────────────");
var vowels = new HashSet<char> { 'a', 'e', 'i', 'o', 'u' };
Console.WriteLine($" Initial vowels: {string.Join(", ", vowels)}");
Console.WriteLine($" Count : {vowels.Count}");
Console.WriteLine();
// ── 2. Add & Remove ─────────────────────────────────────────
Console.WriteLine("── Add / Remove Operations ───────────────────");
// Add returns true if the element was added (not already present)
bool addedUpper = vowels.Add('A');
Console.WriteLine($" Add('A') → added = {addedUpper}");
bool addedDuplicate = vowels.Add('a');
Console.WriteLine($" Add('a') → added = {addedDuplicate} (duplicate — ignored)");
// Remove an element
bool removed = vowels.Remove('A');
Console.WriteLine($" Remove('A') → removed = {removed}");
Console.WriteLine($" Current vowels: {string.Join(", ", vowels)}");
Console.WriteLine();
// ── 3. Set Operations ───────────────────────────────────────
Console.WriteLine("── Set Operations ─────────────────────────────");
var setA = new HashSet<char> { 'a', 'e', 'i', 'o', 'u' };
var setB = new HashSet<char> { 'i', 'o', 'u', 'x', 'y' };
Console.WriteLine($" Set A (vowels) : {string.Join(", ", setA)}");
Console.WriteLine($" Set B (mixed) : {string.Join(", ", setB)}");
// UnionWith — adds all elements from setB into setA
var union = new HashSet<char>(setA);
union.UnionWith(setB);
Console.WriteLine($" Union (A ∪ B) : {string.Join(", ", union)}");
// IntersectWith — keeps only elements present in both sets
var intersect = new HashSet<char>(setA);
intersect.IntersectWith(setB);
Console.WriteLine($" Intersect (A ∩ B): {string.Join(", ", intersect)}");
// ExceptWith — removes elements from setA that are in setB
var difference = new HashSet<char>(setA);
difference.ExceptWith(setB);
Console.WriteLine($" Difference (A−B) : {string.Join(", ", difference)}");
// SymmetricExceptWith — elements in A or B but not both (XOR)
var symmetricDiff = new HashSet<char>(setA);
symmetricDiff.SymmetricExceptWith(setB);
Console.WriteLine($" SymDiff (A △ B) : {string.Join(", ", symmetricDiff)}");
Console.WriteLine();
// ── 4. Membership & Overlap Checks ──────────────────────────
Console.WriteLine("── Membership / Comparison ───────────────────");
Console.WriteLine($" setA.Contains('e') : {setA.Contains('e')}");
Console.WriteLine($" setA.Contains('z') : {setA.Contains('z')}");
Console.WriteLine($" setA.Overlaps(setB) : {setA.Overlaps(setB)}");
Console.WriteLine($" setA.IsSubsetOf(setB) : {setA.IsSubsetOf(setB)}");
Console.WriteLine($" intersect.IsSubsetOf(setA) : {intersect.IsSubsetOf(setA)}");
Console.WriteLine();
// ── 5. Removing Duplicates from a List ──────────────────────
Console.WriteLine("── Removing Duplicates ────────────────────────");
var numbers = new List<int> { 1, 3, 5, 3, 7, 1, 9, 5, 11, 3 };
Console.WriteLine($" Original list: {string.Join(", ", numbers)}");
var uniqueNumbers = new HashSet<int>(numbers);
Console.WriteLine($" Unique values: {string.Join(", ", uniqueNumbers)}");
Console.WriteLine($" Duplicates removed: {numbers.Count - uniqueNumbers.Count}");
Console.WriteLine("\n═══════════════════════════════════════════════");
Console.WriteLine(" Complexity: Add O(1) | Remove O(1) | Contains O(1)");
Console.WriteLine(" Set operations: O(n) where n = size of smaller set");
Console.WriteLine("═══════════════════════════════════════════════");
}
}
}