Rédigé par des étudiants ayant réussi Disponible immédiatement après paiement Lire en ligne ou en PDF Mauvais document ? Échangez-le gratuitement 4,6 TrustPilot
logo-home
Document preview thumbnail
Aperçu 2 sur 8 pages
Autre

Array Manipulation Techniques: Methods and Examples

Document preview thumbnail
Aperçu 2 sur 8 pages

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

Aperçu du contenu

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

Infos sur le Document

Publié le
28 janvier 2025
Nombre de pages
8
Écrit en
2024/2025
Type
Autre
Personne
Inconnu
$6.39

Mauvais document ? Échangez-le gratuitement Dans les 14 jours suivant votre achat et avant le téléchargement, vous pouvez choisir un autre document. Vous pouvez simplement dépenser le montant à nouveau.
Rédigé par des étudiants ayant réussi
Disponible immédiatement après paiement
Lire en ligne ou en PDF

Vendu
0
Abonnés
0
Éléments
252
Dernière vente
-




Pourquoi les étudiants choisissent Stuvia

Créé par d'autres étudiants, vérifié par les avis

Une qualité sur laquelle compter : rédigé par des étudiants qui ont réussi et évalué par d'autres qui ont utilisé ce document.

Le document ne convient pas ? Choisis un autre document

Aucun souci ! Tu peux sélectionner directement un autre document qui correspond mieux à ce que tu cherches.

Paye comme tu veux, apprends aussitôt

Aucun abonnement, aucun engagement. Paye selon tes habitudes par carte de crédit et télécharge ton document PDF instantanément.

Student with book image

“Acheté, téléchargé et réussi. C'est aussi simple que ça.”

Alisha Student

Vous travaillez sur vos références ?

Créez des citations précises en APA, MLA et Harvard avec notre générateur de sources gratuit.

Vous travaillez sur vos références ?

Foire aux questions