Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Document preview thumbnail
Preview 2 out of 8 pages
Other

Array Manipulation Techniques: Methods and Examples

Document preview thumbnail
Preview 2 out of 8 pages

This document explains array manipulation techniques, including sorting, searching, inserting, deleting, and updating array elements. Learn with examples and practical applications.

Content preview

Array Manipulation Techniques
1. Insertion
Inserting an element into an array can be done at the beginning, in the middle, or
at the end of the array. Depending on the position and the array type (static or
dynamic), the approach can vary.

At the End (Efficient):

For dynamic arrays, inserting at the end is typically O(1), as the array is just
extended with the new element.

 Example in Python:

arr.append(10) # Insert 10 at the end of the array

 Example in JavaScript:

arr.push(10); // Insert 10 at the end of the array

At the Beginning (Inefficient):

Inserting at the beginning of an array requires shifting all the elements to the
right, making it O(n).

 Example in Python:

arr.insert(0, 10) # Insert 10 at the beginning of the array

 Example in JavaScript:

arr.unshift(10); // Insert 10 at the beginning of the array

At the Middle (Inefficient):

Insertion at the middle also requires shifting elements, making it O(n). You need
to find the correct position and then shift elements around.

,  Example in Python:

arr.insert(2, 10) # Insert 10 at index 2 (middle)

 Example in JavaScript:

arr.splice(2, 0, 10); // Insert 10 at index 2 (middle)



2. Deletion
Deleting an element from an array can also be done at the beginning, end, or
middle, with varying time complexities depending on the position.

From the End (Efficient):

Deleting an element from the end is typically O(1), as no elements need to be
shifted.

 Example in Python:

arr.pop() # Remove the last element

 Example in JavaScript:

arr.pop(); // Remove the last element

From the Beginning (Inefficient):

Deleting from the beginning requires shifting all the elements to the left, making
it O(n).

 Example in Python:

arr.pop(0) # Remove the first element

 Example in JavaScript:

arr.shift(); // Remove the first element

Document information

Uploaded on
January 28, 2025
Number of pages
8
Written in
2024/2025
Type
Other
Person
Unknown
$6.39

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Sold
0
Followers
0
Items
252
Last sold
-




Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions