Skip to content

Instantly share code, notes, and snippets.

@pingkunga
Created February 28, 2025 00:57
Show Gist options
  • Save pingkunga/2d0f05a3a9b3f6fb3a3530a2630b6d90 to your computer and use it in GitHub Desktop.
Save pingkunga/2d0f05a3a9b3f6fb3a3530a2630b6d90 to your computer and use it in GitHub Desktop.
# Data Structures: Big O Complexity, Use-Case, and Sample Code
# C# Data Structures: Big O Complexity, Use-Case, and Sample Code
## Array
### Big O Complexity
- Access: O(1)
- Search: O(n)
- Insert: O(n)
- Delete: O(n)
### Use-Case
Arrays are used when you need fast access to elements using an index and when the size of the data set is known and fixed.
### Sample Code
```csharp
int[] numbers = { 1, 2, 3, 4, 5 };
Console.WriteLine(numbers[2]); // Access element at index 2
```
## List
### Big O Complexity
- Access: O(1)
- Search: O(n)
- Insert: O(n) (O(1) if adding at the end)
- Delete: O(n)
### Use-Case
Lists are used when you need a dynamic array that can grow and shrink in size.
### Sample Code
```csharp
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
numbers.Add(6); // Insert
numbers.Remove(3); // Delete
```
## Dictionary
### Big O Complexity
- Access: O(1)
- Search: O(1)
- Insert: O(1)
- Delete: O(1)
### Use-Case
Dictionaries are used when you need to store key-value pairs and require fast lookups by key.
### Sample Code
```csharp
Dictionary<string, int> ages = new Dictionary<string, int>
{
{ "Alice", 30 },
{ "Bob", 25 }
};
Console.WriteLine(ages["Alice"]); // Access
```
## HashSet
### Big O Complexity
- Access: O(1)
- Search: O(1)
- Insert: O(1)
- Delete: O(1)
### Use-Case
HashSets are used when you need to store unique elements and require fast lookups.
### Sample Code
```csharp
HashSet<int> uniqueNumbers = new HashSet<int> { 1, 2, 3, 4, 5 };
uniqueNumbers.Add(6); // Insert
uniqueNumbers.Remove(3); // Delete
```
## Stack
### Big O Complexity
- Access: O(n)
- Search: O(n)
- Insert: O(1)
- Delete: O(1)
### Use-Case
Stacks are used when you need a Last-In-First-Out (LIFO) data structure.
### Sample Code
```csharp
Stack<int> stack = new Stack<int>();
stack.Push(1); // Insert
stack.Push(2); // Insert
Console.WriteLine(stack.Pop()); // Delete and Access
```
## Queue
### Big O Complexity
- Access: O(n)
- Search: O(n)
- Insert: O(1)
- Delete: O(1)
### Use-Case
Queues are used when you need a First-In-First-Out (FIFO) data structure.
### Sample Code
```csharp
Queue<int> queue = new Queue<int>();
queue.Enqueue(1); // Insert
queue.Enqueue(2); // Insert
Console.WriteLine(queue.Dequeue()); // Delete and Access
```
## LinkedList
### Big O Complexity
- Access: O(n)
- Search: O(n)
- Insert: O(1)
- Delete: O(1)
### Use-Case
LinkedLists are used when you need a dynamic data structure with efficient insertions and deletions.
### Sample Code
```csharp
LinkedList<int> linkedList = new LinkedList<int>();
linkedList.AddLast(1); // Insert
linkedList.AddLast(2); // Insert
linkedList.Remove(1); // Delete
```
## SortedList
### Big O Complexity
- Access: O(log n)
- Search: O(log n)
- Insert: O(n)
- Delete: O(n)
### Use-Case
SortedLists are used when you need a collection that maintains its elements in a sorted order.
### Sample Code
```csharp
SortedList<int, string> sortedList = new SortedList<int, string>();
sortedList.Add(1, "One"); // Insert
sortedList.Add(2, "Two"); // Insert
Console.WriteLine(sortedList[1]); // Access
```
## SortedDictionary
### Big O Complexity
- Access: O(log n)
- Search: O(log n)
- Insert: O(log n)
- Delete: O(log n)
### Use-Case
SortedDictionaries are used when you need a dictionary that maintains its elements in a sorted order by keys.
### Sample Code
```csharp
SortedDictionary<int, string> sortedDictionary = new SortedDictionary<int, string>();
sortedDictionary.Add(1, "One"); // Insert
sortedDictionary.Add(2, "Two"); // Insert
Console.WriteLine(sortedDictionary[1]); // Access
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment