π LeetCode 0: Array CRUD Operations & Architecture
π Problem Overview
- Problem Number: 0
- Problem Name: Array CRUD Operations & Architecture
- Difficulty: Fundamentals
- Category: Arrays and Hashing
- Runnable Source:
Program.cs
π‘ Algorithmic Approach & Pattern
This problem is solved using the Arrays and Hashing algorithmic pattern.
Key Insights & Invariants:
- Identify the input constraints and edge conditions.
- Structure the data flow to minimize unnecessary traversals and heap reallocations.
- Validate corner cases (empty collections, single-element cases, duplicates, extremes).
π» Verified C# Implementation (.NET 9)
The solution is verified and executable via the companion project in src/zArray-crud/:
// Test: Add value at index 2
int[] addInput = new int[] { 1, 2, 3, 4, 5, 1, 5 };
Console.WriteLine("Test: Add value 99 at index 2");
Console.WriteLine("Input: " + ArrayToString(addInput));
int[] addResult = AddAtIndex(addInput, 99, 2);
Console.WriteLine("Output: " + ArrayToString(addResult));
Console.WriteLine();
// Test: Delete first occurrence of value 1
int[] deleteFirstInput = new int[] { 1, 2, 3, 4, 5, 1, 5 };
Console.WriteLine("Test: Delete first occurrence of value 1");
Console.WriteLine("Input: " + ArrayToString(deleteFirstInput));
int[] deleteFirstResult = DeleteFirstOccurrence(deleteFirstInput, 1);
Console.WriteLine("Output: " + ArrayToString(deleteFirstResult));
Console.WriteLine();
// Test: Delete by index 2
int[] deleteByIndexInput = new int[] { 1, 2, 3, 4, 5, 1, 5 };
Console.WriteLine("Test: Delete value at index 2");
Console.WriteLine("Input: " + ArrayToString(deleteByIndexInput));
int[] deleteByIndexResult = DeleteAtIndex(deleteByIndexInput, 2);
Console.WriteLine("Output: " + ArrayToString(deleteByIndexResult));
Console.WriteLine();
// Test: Delete all occurrences of value 5
int[] deleteAllInput = new int[] { 1, 2, 3, 4, 5, 1, 5 };
Console.WriteLine("Test: Delete all occurrences of value 5");
Console.WriteLine("Input: " + ArrayToString(deleteAllInput));
int[] deleteAllResult = DeleteAllOccurrences(deleteAllInput, 5);
Console.WriteLine("Output: " + ArrayToString(deleteAllResult));
Console.WriteLine();
// Test: Update value at index 3
int[] updateInput = new int[] { 1, 2, 3, 4, 5, 1, 5 };
Console.WriteLine("Test: Update value at index 3 to 99");
Console.WriteLine("Input: " + ArrayToString(updateInput));
int[] updateResult = Update((int[])updateInput.Clone(), 99, 3);
Console.WriteLine("Output: " + ArrayToString(updateResult));
Console.WriteLine();
// Test: Update all occurrences of value 1 to 88
int[] updateAllInput = new int[] { 1, 2, 3, 4, 5, 1, 5 };
Console.WriteLine("Test: Update all occurrences of value 1 to 88");
Console.WriteLine("Input: " + ArrayToString(updateAllInput));
int[] updateAllResult = UpdateAll((int[])updateAllInput.Clone(), 88, 1);
Console.WriteLine("Output: " + ArrayToString(updateAllResult));
Console.WriteLine();
// Test: Get all indices of value 5
int[] getInput = new int[] { 1, 2, 3, 4, 5, 1, 5 };
Console.WriteLine("Test: Get all indices of value 5");
Console.WriteLine("Input: " + ArrayToString(getInput));
var getResult = Get(getInput, 5);
Console.WriteLine("Output: " + string.Join(", ", getResult.Select(kvp => $"[{kvp.Key}]={kvp.Value}")));
Console.WriteLine();
string ArrayToString(int[] array)
{
return string.Join(" ", array);
}
// Add value at specific index
int[] AddAtIndex(int[] array, int value, int index)
{
int[] newArray = new int[array.Length + 1];
for (int i = 0; i < index; i++)
{
newArray[i] = array[i];
}
newArray[index] = value;
for (int i = index; i < array.Length; i++)
{
newArray[i + 1] = array[i];
}
return newArray;
}
// Delete first occurrence of value
int[] DeleteFirstOccurrence(int[] array, int value)
{
int index = -1;
for (int i = 0; i < array.Length; i++)
{
if (array[i] == value)
{
index = i;
break;
}
}
if (index == -1) return array;
int[] newArray = new int[array.Length - 1];
for (int i = 0; i < index; i++)
{
newArray[i] = array[i];
}
for (int i = index + 1; i < array.Length; i++)
{
newArray[i - 1] = array[i];
}
return newArray;
}
// Delete all occurrences of value
int[] DeleteAllOccurrences(int[] array, int value)
{
int count = 0;
for (int i = 0; i < array.Length; i++)
{
if (array[i] == value)
{
count++;
}
}
int[] newArray = new int[array.Length - count];
int idx = 0;
for (int i = 0; i < array.Length; i++)
{
if (array[i] != value)
{
newArray[idx++] = array[i];
}
}
return newArray;
}
// Delete value at specific index
int[] DeleteAtIndex(int[] array, int index)
{
if (index < 0 || index >= array.Length) return array;
int[] newArray = new int[array.Length - 1];
for (int i = 0; i < index; i++)
{
newArray[i] = array[i];
}
for (int i = index + 1; i < array.Length; i++)
{
newArray[i - 1] = array[i];
}
return newArray;
}
// write a method to update a value in an array at a specific location
int[] Update(int[] array, int value, int index)
{
array[index] = value;
return array;
}
int[] UpdateAll(int[] array, int values, int replaceValue)
{
for (int i = 0; i < array.Length; i++)
{
if (array[i] == replaceValue)
{
array[i] = values;
}
}
return array;
}
// write a method to get a value from an array at a specific location
Dictionary<int, int> Get(int[] array, int value)
{
var dict = new Dictionary<int, int>();
for (int i = 0; i < array.Length; i++)
{
if (array[i] == value)
{
dict.Add(i, value);
}
}
return dict;
}π Complexity Analysis
| Metric | Complexity | Notes |
|---|---|---|
| Time Complexity | (or optimal for pattern) | Single or bounded passes over the input |
| Space Complexity | or | Minimal auxiliary memory overhead |
π¨ Edge Cases & Interview Traps
- Empty / Null Input: Ensure null safety and guard clauses.
- Single Element: Confirm the algorithm terminates without out-of-bounds indexing.
- Duplicates & Boundaries: Check duplicate handling and extreme integer limits.