1D Arrays Operations šŸš€

beginner
23 min

1D Arrays Operations šŸš€

Welcome to our comprehensive guide on 1D Arrays Operations! This lesson is designed to help you understand and master the fundamental operations of 1D arrays, including Traversal, Insertion, Deletion, and Search. Let's dive in! šŸŽÆ

What is a 1D Array? šŸ“

A 1D array is a collection of elements, each of the same data type, stored in contiguous memory locations. It's like a line of boxes, each box containing a single item.

Creating a 1D Array šŸ’”

In JavaScript, you can create a 1D array using an array literal:

javascript
let myArray = [1, 2, 3, 4, 5];

In Python, it's similar:

python
my_array = [6, 7, 8, 9, 10]

Traversing a 1D Array šŸ”

Traversing means going through each element in the array one by one. We'll use a loop to do this.

javascript
let myArray = [1, 2, 3, 4, 5]; for(let i = 0; i < myArray.length; i++) { console.log(myArray[i]); }

In Python:

python
my_array = [6, 7, 8, 9, 10] for i in range(len(my_array)): print(my_array[i])

Insertion in a 1D Array šŸ’”

Inserting an element into a 1D array involves finding the correct position and shifting the elements to make room.

javascript
let myArray = [1, 2, 3, 4, 5]; myArray.splice(2, 0, 0); // Inserts 0 at index 2

In Python:

python
my_array = [6, 7, 8, 9, 10] my_array.insert(2, 0) // Inserts 0 at index 2

Deletion from a 1D Array šŸ’”

Deleting an element from a 1D array involves removing the element at the specified index.

javascript
let myArray = [1, 2, 3, 4, 5]; myArray.splice(2, 1); // Removes element at index 2

In Python:

python
my_array = [6, 7, 8, 9, 10] del my_array[2] // Removes element at index 2

Searching in a 1D Array šŸ”

Searching for an element in a 1D array involves checking each element until we find the target.

javascript
let myArray = [1, 2, 3, 4, 5]; function findElement(arr, target) { for(let i = 0; i < arr.length; i++) { if(arr[i] === target) { return i; } } return -1; }

In Python:

python
my_array = [6, 7, 8, 9, 10] def find_element(arr, target): for i in range(len(arr)): if arr[i] == target: return i return -1
Quick Quiz
Question 1 of 1

What operation is used to remove an element from a 1D array in JavaScript?

That's it for our introductory lesson on 1D Arrays Operations! Stay tuned for more advanced topics like Multi-dimensional Arrays, Linked Lists, and Algorithms. Happy coding! šŸŽ‰