Welcome to Day 3 of our Data Structures and Algorithms (DSA) series! Today, we'll delve into one of the most fundamental data structures: Arrays. By the end of this post, you'll have a solid understanding of arrays, their properties, and common operations performed on them. Let's get started! π
please subscribe to my YouTube channel to support my channel and get more web development tutorials.
What is an Array? π€
An array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of the same type together. This makes it easier to calculate the position of each element by simply adding an offset to a base value, i.e., the memory location of the first element of the array (generally denoted by the name of the array).
Key Characteristics of Arrays
- Fixed Size: Arrays have a fixed size, which means the number of elements is determined when the array is created.
- Indexed Access: Elements in an array are accessed using an index. The first element is at index 0.
- Homogeneous Elements: All elements in an array are of the same type.
How to Declare and Initialize Arrays in JavaScript π
Declaration
In JavaScript, you can declare an array using square brackets []
or the Array
constructor.
// Using square brackets
let arr1 = [];
// Using Array constructor
let arr2 = new Array();
Initialization
You can initialize arrays with values at the time of declaration.
let numbers = [1, 2, 3, 4, 5];
let fruits = ["apple", "banana", "cherry"];
Common Operations on Arrays π οΈ
1. Traversing an Array
To traverse an array means to access each element of the array exactly once.
let arr = [10, 20, 30, 40, 50];
for (let i = 0; i < arr.length; i++) {
console.log(arr[i]);
}
2. Inserting Elements
In JavaScript, you can use methods like push
, unshift
, and splice
to insert elements into an array.
let arr = [10, 20, 30];
// Inserting at the end
arr.push(40); // [10, 20, 30, 40]
// Inserting at the beginning
arr.unshift(0); // [0, 10, 20, 30, 40]
// Inserting at a specific position
arr.splice(2, 0, 15); // [0, 10, 15, 20, 30, 40]
3. Deleting Elements
To delete elements from an array, you can use methods like pop
, shift
, and splice
.
let arr = [10, 20, 30, 40, 50];
// Deleting from the end
arr.pop(); // [10, 20, 30, 40]
// Deleting from the beginning
arr.shift(); // [20, 30, 40]
// Deleting from a specific position
arr.splice(1, 1); // [20, 40]
4. Searching for Elements
You can search for elements in an array using methods like indexOf
and includes
.
let arr = [10, 20, 30, 40, 50];
// Searching for an element
let index = arr.indexOf(30); // 2
let exists = arr.includes(40); // true
5. Updating Elements
To update elements in an array, you simply access the element by its index and assign a new value.
let arr = [10, 20, 30, 40, 50];
arr[2] = 35; // [10, 20, 35, 40, 50]
Time Complexity of Array Operations β±οΈ
Understanding the time complexity of array operations helps in writing efficient code.
- Accessing Elements: O(1)
- Inserting/Deleting at the End: O(1)
- Inserting/Deleting at the Beginning: O(n)
- Inserting/Deleting at an Arbitrary Position: O(n)
- Searching for an Element: O(n)
Conclusion π―
Today, we explored arrays, one of the most fundamental data structures. We learned how to declare, initialize, and perform common operations on arrays. Understanding arrays is crucial as they form the basis for more complex data structures and algorithms.
Series Index
Part | Title | Link |
---|---|---|
1 | Day 1: Introduction to Data Structures and Algorithms (DSA)π | Read |
2 | Day 2: Understanding Big O Notation π | Read |
3 | Day 3: Introduction to Arrays π | Read |
4 | Day 4: Understanding Linked Lists π | Read |
Stay tuned for Day 4, where we will dive into Linked Lists, their properties, and operations. Feel free to leave your comments or questions below. If you found this guide helpful, please share it with your peers and follow me for more web development tutorials.
π Happy Coding!
Follow and Subscribe:
- Instagram: devdivewithdipak
- Website: Dipak Ahirav
- Email: dipaksahirav@gmail.com
- YouTube: devDive with Dipak
- LinkedIn: Dipak Ahirav
Top comments (0)