DEV Community

Cover image for Day 3: Introduction to Arrays πŸ“‹
Dipak Ahirav
Dipak Ahirav

Posted on • Edited on

Day 3: Introduction to Arrays πŸ“‹

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();
Enter fullscreen mode Exit fullscreen mode

Initialization

You can initialize arrays with values at the time of declaration.

let numbers = [1, 2, 3, 4, 5];
let fruits = ["apple", "banana", "cherry"];
Enter fullscreen mode Exit fullscreen mode

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]);
}
Enter fullscreen mode Exit fullscreen mode

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]
Enter fullscreen mode Exit fullscreen mode

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]
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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]
Enter fullscreen mode Exit fullscreen mode

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:

Top comments (0)