Skip to content

Commit bb32534

Browse files
Update Collections Topic
1 parent ef9d79c commit bb32534

8 files changed

Lines changed: 700 additions & 104 deletions

File tree

docs/programming-languages/collections/_c.mdx

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,20 @@ int find(int element, int size, int my_array[]) // Just an example
9494
// ------------------------------------
9595
// Comparing
9696
// ------------------------------------
97-
// TODO
97+
// Not Available
98+
// You need to create your own logic
99+
bool is_equal(int size, int first_array[], int second_array[])
100+
{
101+
for(int i = 0; i < size; i++)
102+
{
103+
if (first_array[i] != second_array[i])
104+
{
105+
return false;
106+
}
107+
}
108+
109+
return true;
110+
}
98111

99112

100113
// ------------------------------------
@@ -135,7 +148,27 @@ int32_t multi_dimensional_array[20][30];
135148
```C
136149
// Dynamic Size Arrays
137150
138-
// TODO
151+
// No Native Support.
152+
// Use malloc(), realloc(), and free(), or wrap this logic in a struct.
153+
154+
typedef struct IntArray
155+
{
156+
int *items;
157+
int size;
158+
int capacity;
159+
} IntArray;
160+
161+
void push(IntArray *array, int value)
162+
{
163+
if (array->size == array->capacity)
164+
{
165+
array->capacity = array->capacity == 0 ? 4 : array->capacity * 2;
166+
array->items = realloc(array->items, array->capacity * sizeof(int));
167+
}
168+
169+
array->items[array->size] = value;
170+
array->size++;
171+
}
139172
```
140173

141174
```C
Lines changed: 91 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,150 @@
11
```Cs
2+
// Array
3+
24
// ------------------------------------
35
// Declaration & Initialization
46
// ------------------------------------
5-
6-
// TODO
7+
int[] myArray = new int[5];
8+
int[] myArray = { 1, 2, 3 };
9+
var myArray = new[] { 1, 2, 3 };
710

811
// Const Declaration
9-
// TODO
12+
// Arrays are mutable. Use readonly fields or immutable collections when needed.
13+
readonly int[] myArray = { 1, 2, 3 };
1014

1115
// ------------------------------------
1216
// Direct Access
1317
// ------------------------------------
14-
// TODO
18+
myArray[0] = 14;
19+
int value = myArray[0];
1520

1621
// ------------------------------------
1722
// Push & Pop
1823
// ------------------------------------
19-
// TODO
24+
// Not Available for fixed-size arrays.
25+
// Use List<T> for dynamic collections.
2026
2127
// ------------------------------------
2228
// Size / Length
2329
// ------------------------------------
24-
// TODO
30+
int length = myArray.Length;
2531

2632
// ------------------------------------
2733
// Count (How many of)
2834
// ------------------------------------
35+
int howMany = myArray.Count(element => element == target);
2936

3037
// ------------------------------------
3138
// Filter
3239
// ------------------------------------
33-
// TODO
40+
int[] filtered = myArray.Where(element => element > 10).ToArray();
3441

3542
// ------------------------------------
3643
// Find (return Index)
3744
// ------------------------------------
38-
// TODO
45+
int index = Array.IndexOf(myArray, target);
46+
int index = Array.FindIndex(myArray, element => element > 10);
3947

4048
// ------------------------------------
4149
// Accumulate (sum all values)
4250
// ------------------------------------
43-
// TODO
51+
int total = myArray.Sum();
52+
int total = myArray.Aggregate(0, (result, element) => result + element);
4453

4554
// ------------------------------------
4655
// Sort
4756
// ------------------------------------
48-
// TODO
57+
Array.Sort(myArray);
58+
Array.Sort(myArray, (first, second) => second.CompareTo(first));
4959

5060
// ------------------------------------
5161
// Comparing
5262
// ------------------------------------
53-
// TODO
63+
bool isEqual = myArray.SequenceEqual(otherArray);
5464

5565
// ------------------------------------
5666
// Copy / Clone
5767
// ------------------------------------
58-
// TODO
68+
int[] copy = myArray.ToArray();
69+
int[] copy = (int[])myArray.Clone();
5970

6071
// ------------------------------------
6172
// Slice
6273
// ------------------------------------
63-
// TODO
74+
int[] slice = myArray[1..3]; // Since C# 8.0
6475
6576
// ------------------------------------
6677
// Printing Members
6778
// ------------------------------------
68-
// TODO
79+
Console.WriteLine(string.Join(", ", myArray));
80+
81+
foreach (int element in myArray)
82+
{
83+
Console.WriteLine(element);
84+
}
6985

7086
// ------------------------------------
7187
// As a function Parameter
7288
// ------------------------------------
73-
// TODO
89+
void MyFunction(int[] values)
90+
{
91+
// ...
92+
}
93+
```
94+
95+
```Cs
96+
// List
97+
98+
List<int> myList = new List<int>();
99+
List<int> myList = new() { 1, 2, 3 };
100+
101+
myList[0] = 14;
102+
myList.Add(10);
103+
myList.Insert(0, 10);
104+
myList.RemoveAt(0);
105+
myList.Remove(10);
106+
107+
int length = myList.Count;
108+
int howMany = myList.Count(element => element == target);
109+
List<int> filtered = myList.Where(element => element > 10).ToList();
110+
int index = myList.FindIndex(element => element > 10);
111+
int total = myList.Sum();
112+
113+
myList.Sort();
114+
myList.Sort((first, second) => second.CompareTo(first));
115+
116+
bool isEqual = myList.SequenceEqual(otherList);
117+
List<int> copy = new List<int>(myList);
118+
List<int> slice = myList.GetRange(1, 2);
119+
120+
Console.WriteLine(string.Join(", ", myList));
121+
122+
void MyFunction(List<int> values)
123+
{
124+
// ...
125+
}
126+
```
127+
128+
```Cs
129+
// Dictionary
130+
131+
Dictionary<string, int> myDictionary = new Dictionary<string, int>();
132+
Dictionary<string, int> myDictionary = new()
133+
{
134+
["first"] = 1,
135+
["second"] = 2,
136+
};
137+
138+
myDictionary["third"] = 3;
139+
int value = myDictionary["first"];
140+
bool hasKey = myDictionary.ContainsKey("first");
141+
bool found = myDictionary.TryGetValue("first", out int value);
142+
143+
myDictionary.Remove("first");
144+
int length = myDictionary.Count;
145+
146+
foreach (KeyValuePair<string, int> item in myDictionary)
147+
{
148+
Console.WriteLine($"{item.Key}: {item.Value}");
149+
}
74150
```

0 commit comments

Comments
 (0)