File tree Expand file tree Collapse file tree 1 file changed +61
-0
lines changed Expand file tree Collapse file tree 1 file changed +61
-0
lines changed Original file line number Diff line number Diff line change
1
+ //Sorted Set elements must be unique
2
+ //Sorted Set sorts elements in ascending order
3
+ using System ;
4
+ using System . Collections . Generic ;
5
+
6
+ class SortedSet {
7
+ public static void Main ( )
8
+ {
9
+ //Instantiate a Sorted Set
10
+ SortedSet < int > omars_Set = new SortedSet < int > ( ) ;
11
+
12
+ //Pushing elements to
13
+ omars_Set . Add ( 68 ) ;
14
+ omars_Set . Add ( 98 ) ;
15
+ omars_Set . Add ( 32 ) ;
16
+ omars_Set . Add ( 1 ) ;
17
+ omars_Set . Add ( 1174 ) ;
18
+ Console . WriteLine ( "Elements of omars_Set:" ) ;
19
+
20
+ foreach ( var elem in omars_Set )
21
+ {
22
+ Console . WriteLine ( elem ) ;
23
+ }
24
+
25
+ //Creating a sorted setwith initial values
26
+ SortedSet < int > omars_Set2 = new SortedSet < int > ( ) {
27
+ 98 , 9 , 909 , 45 } ;
28
+
29
+ Console . WriteLine ( "Elements of my_Set2:" ) ;
30
+ foreach ( var valu in omars_Set2 )
31
+ {
32
+ Console . WriteLine ( valu ) ;
33
+ }
34
+
35
+ //To remove an element from a set call the .Remove method with the value you want to remove
36
+ omars_Set2 . Remove ( 909 ) ;
37
+ Console . WriteLine ( "Updated omars_Set2: " ) ;
38
+
39
+ //Unexpected symbol `foreach' means you forgot a semi-colon
40
+ foreach ( var updSet2 in omars_Set2 )
41
+ {
42
+ Console . WriteLine ( updSet2 ) ;
43
+ }
44
+
45
+
46
+ //Checking whether an element exists
47
+ if ( omars_Set2 . Contains ( 909 ) == true )
48
+ {
49
+ Console . WriteLine ( "Element Exists" ) ;
50
+ }
51
+
52
+ else
53
+ {
54
+ Console . WriteLine ( "Element is not there" ) ;
55
+ }
56
+ //Empty out the set
57
+ omars_Set2 . Clear ( ) ;
58
+
59
+ Console . WriteLine ( "The total number of elements within the set after performing the .Clear method:{0}" , omars_Set2 . Count ) ;
60
+ }
61
+ }
You can’t perform that action at this time.
0 commit comments