Overview
Data Structures
Searching
An Array is a data structure that stores a collection of elements, each identified by an index or a key. It allows efficient access to elements by their index and is one of the most commonly used data structures in programming.
Arrays have a fixed size, meaning that once defined, the size of the array cannot be changed. This makes arrays simple but less flexible when the number of elements is unknown or dynamic.
// Example in C (Array)
int arr[5] = {1, 2, 3, 4, 5};
printf("%d", arr[2]); // Output: 3A Static Array is an array that has a fixed size, defined at the time of its creation. The size cannot be changed during runtime, and if the number of elements exceeds the allocated size, an overflow occurs.
Static arrays are used when the size of the dataset is known beforehand and will not change. They provide constant-time access to elements but do not handle dynamic data well.
// Example in C (Static Array)
// In C, static arrays are declared with a fixed size.
int arr[5] = {1, 2, 3, 4, 5};
printf("%d", arr[2]); // Output: 3Set the fixed size of your array. Creating a new array replaces the current one.
A Static Array is used when the amount of data is fixed and does not change over time. This means you know in advance how many elements you need to store.
Imagine you are creating a program to store the days of the week: "Monday, Tuesday, ..., Sunday." Since the number of days is always 7 and will not change, a static array is a good choice.
Static arrays are simple to use but cannot be resized once created. If you try to store more elements than the fixed size, the program will run into an error. That's why they work well when you are sure about the size of your data.
// Static Array Example let daysOfWeek = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]; console.log(daysOfWeek[0]); // Output: "Monday"
A Dynamic Array is a data structure that resizes itself automatically when the number of elements exceeds its current capacity. Unlike static arrays, dynamic arrays provide flexibility by adjusting their size during runtime.
Dynamic arrays are suitable for scenarios where the number of elements is unknown beforehand. They maintain constant-time access to elements while providing dynamic resizing capabilities.
// Example in JavaScript
class DynamicArray {
constructor() {
this.array = [];
this.size = 0;
}
add(element) {
this.array[this.size] = element;
this.size++;
}
}
const dynamicArray = new DynamicArray();
dynamicArray.add(10);
dynamicArray.add(20);
console.log(dynamicArray.size); // Output: 2Create a new dynamic array with an initial size. Creating a new array replaces the current one.
A Dynamic Array is used when the amount of data can change over time. This means you don’t know how many elements you need to store when the program starts.
For example, consider an online meeting. At the start, there might be no participants. As people join the meeting, their names need to be added. Similarly, when someone leaves the meeting, their name should be removed. A dynamic array is perfect for this because it can grow or shrink as needed.
// Dynamic Array Example
let participants = []; // Initially, the array is empty.
participants.push("Alice"); // Alice joins the meeting.
participants.push("Bob"); // Bob joins the meeting.
console.log(participants); // Output: ["Alice", "Bob"]
participants.pop(); // Bob leaves the meeting.
console.log(participants); // Output: ["Alice"]