Skip to main content

Arrays in Data Structures and Algorithms

An array is a collection of items stored at contiguous memory locations. It is a data structure that stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.

Visualizations of Arrays in Data Structures and Algorithms (DSA)

Speed:

 

Lowest value:



Why are Arrays important?

Arrays are important because they allow us to store multiple items of the same type in a single variable. They are used to store data in a structured way, and they are used in many algorithms and data structures.

How to declare an Array?

An array can be declared in various programming languages using the following syntax:

Written by @Ajay-Dhangar
// Declare an array in JavaScript
let arr = [1, 2, 3, 4, 5];

How to access an Array?

An array can be accessed using the index of the element. The index of the first element is 0, the index of the second element is 1, and so on.

Written by @Ajay-Dhangar
// Access an array in JavaScript
let arr = [1, 2, 3, 4, 5];
console.log(arr[0]); // 1
console.log(arr[1]); // 2
console.log(arr[2]); // 3
console.log(arr[3]); // 4
console.log(arr[4]); // 5

How to update an Array?

An array can be updated by assigning a new value to the index of the element.

Written by @Ajay-Dhangar
// Update an array in JavaScript
let arr = [1, 2, 3, 4, 5];
arr[0] = 10;
console.log(arr); // [10, 2, 3, 4, 5]

How to find the length of an Array?

The length of an array can be found using the length property.

Written by @Ajay-Dhangar
// Find the length of an array in JavaScript
let arr = [1, 2, 3, 4, 5];
console.log(arr.length); // 5

How to iterate through an Array?

An array can be iterated using a loop such as for loop, while loop, or for...of loop.

Written by @Ajay-Dhangar
// Iterate through an array in JavaScript
let arr = [1, 2, 3, 4, 5];
for (let i = 0; i < arr.length; i++) {
console.log(arr[i]);
}
// 1
// 2
// 3
// 4
// 5

How to find the maximum and minimum elements in an Array?

The maximum and minimum elements in an array can be found by iterating through the array and comparing each element with the current maximum and minimum elements.

Written by @Ajay-Dhangar
// Find the maximum and minimum elements in an array in JavaScript
function findMaxMin(arr) {
let max = arr[0];
let min = arr[0];
for (let i = 1; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
if (arr[i] < min) {
min = arr[i];
}
}
return { max, min };
}

let arr = [2, 5, 1, 20, 10];
console.log(findMaxMin(arr)); // { max: 20, min: 1 }
📝 Info
  • The time complexity of finding the maximum and minimum elements in an array is O(n).
  • The space complexity of finding the maximum and minimum elements in an array is O(1).

Problems for Practice

Start Practicing: https://leetcode.com/study-plan/programming-skills

Problem NameDifficultySolution Link
1. Two SumEasyView Solutions
2. Best Time to Buy and Sell StockEasyView Solutions
3. Contains DuplicateEasyView Solutions
4. Product of Array Except SelfMediumView Solutions
5. Maximum SubarrayEasyView Solutions

Conclusion

In this tutorial, we learned about arrays in data structures and algorithms. We learned how to declare an array, access an array, update an array, find the length of an array, iterate through an array, and find the maximum and minimum elements in an array. Arrays are an important data structure that is used in many algorithms and data structures.