Open In App

Insert Element at the End of an Array

Last Updated : 07 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Given an array of integers, the task is to insert an element at the end of the array.

Examples:

Input: arr[] = [10, 20, 30, 40], ele = 50
Output: [10, 20, 30, 40, 50]

Input: arr[] = [], ele = 20
Output: [20]

[Approach 1] Using Built-In Methods

We will use library methods like push_back() in C++, add() in Java and C#, append() in Python and push() in JavaScript.

C++
// C++ program to insert given element at the end 
// of an array using built-in methods

#include <iostream>
#include <vector>
using namespace std;

int main() {
    vector<int> arr = {10, 20, 30, 40};
  	int ele = 50;
    cout << "Array before insertion\n";
    for (int i = 0; i < arr.size(); i++)
        cout << arr[i] << " ";
	
  	// Insert element at the end
    arr.push_back(ele);

    cout << "\nArray after insertion\n";
    for (int i = 0; i < arr.size(); i++)
        cout << arr[i] << " ";

    return 0;
}
Java
// Java program to insert a given element at the end of an array
// using built-in methods

import java.util.ArrayList;
import java.util.Arrays;

class GfG {
    public static void main(String[] args) {
        ArrayList<Integer> arr = new 
          			ArrayList<>(Arrays.asList(10, 20, 30, 40));
        int ele = 50;
        System.out.println("Array before insertion");
        for (int i = 0; i < arr.size(); i++)
            System.out.print(arr.get(i) + " ");

        // Insert element at the end
        arr.add(ele);

        System.out.println("\nArray after insertion");
        for (int i = 0; i < arr.size(); i++)
            System.out.print(arr.get(i) + " ");
    }
}
Python
# Python program to insert a given element at the end 
# of an array using built-in methods

if __name__ == "__main__":
    arr = [10, 20, 30, 40]
    ele = 50
    print("Array before insertion")
    for i in range(len(arr)):
        print(arr[i], end=" ")

    # Insert element at the end
    arr.append(ele)

    print("\nArray after insertion")
    for i in range(len(arr)):
        print(arr[i], end=" ")
C#
// C# program to insert a given element at the end 
// of an array using built-in methods

using System;
using System.Collections.Generic;

class GfG {
    static void Main() {
        List<int> arr = new List<int> { 10, 20, 30, 40 };
        int ele = 50;
        Console.WriteLine("Array before insertion");
        foreach (int num in arr)
            Console.Write(num + " ");

        // Insert element at the end
        arr.Add(ele);

        Console.WriteLine("\nArray after insertion");
        foreach (int num in arr)
            Console.Write(num + " ");
    }
}
JavaScript
// JavaScript program to insert a given element at the end 
// of an array using built-in methods

let arr = [10, 20, 30, 40];
let ele = 50;
console.log("Array before insertion");
console.log(arr.join(" "));

// Insert element at the end
arr.push(ele);

console.log("Array after insertion");
console.log(arr.join(" "));

Output
Array before insertion
10 20 30 40 
Array after insertion
10 20 30 40 50 

Time Complexity: O(1)
Auxiliary Space: O(1)

[Approach 2] Using Custom Method

To insert an element at the end of an array, we can simply add the new element at the nth index.

C++
// C++ program to insert given element at the end 
// of an array using custom methods

#include <iostream>
#include <vector>
using namespace std;

int main() {
    int n = 4;
    vector<int> arr = {10, 20, 30, 40, 0};
  	int ele = 50;
    cout << "Array before insertion\n";
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
	
  	arr[n] = ele;

    cout << "\nArray after insertion\n";
    for (int i = 0; i <= n; i++)
        cout << arr[i] << " ";

    return 0;
}
C
// C program to insert given element at the end 
// of an array using custom methods

#include <stdio.h>

int main() {
    int n = 4;
    int arr[5] = {10, 20, 30, 40, 0};
    int ele = 50;
    
    printf("Array before insertion\n");
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }

    // Inserting element at the end of the array
    arr[n] = ele;

    printf("\nArray after insertion\n");
    for (int i = 0; i <= n; i++) {
        printf("%d ", arr[i]);
    }

    return 0;
}
Java
// Java program to insert given element at the end 
// of an array using custom methods

class GfG {
    public static void main(String[] args) {
        int n = 4;
        int[] arr = {10, 20, 30, 40, 0};
        int ele = 50;

        System.out.println("Array before insertion");
        for (int i = 0; i < n; i++) {
            System.out.print(arr[i] + " ");
        }

        // Inserting element at the end of the array
        arr[n] = ele;

        System.out.println("\nArray after insertion");
        for (int i = 0; i <= n; i++) {
            System.out.print(arr[i] + " ");
        }
    }
}
Python
# Python program to insert given element at the end
# of an array using custom methods

if __name__ == "__main__":
    n = 4
    arr = [10, 20, 30, 40, 0]
    ele = 50

    print("Array before insertion")
    for i in range(n):
        print(arr[i], end=" ")

    # Inserting element at the end of the array
    arr[n] = ele

    print("\nArray after insertion")
    for i in range(n + 1):
        print(arr[i], end=" ")
C#
using System;

class GfG {
    static void Main() {
        int n = 4;
        int[] arr = { 10, 20, 30, 40, 0 };
        int ele = 50;

        Console.WriteLine("Array before insertion");
        for (int i = 0; i < n; i++) {
            Console.Write(arr[i] + " ");
        }

        // Inserting element at the end of the array
        arr[n] = ele;

        Console.WriteLine("\nArray after insertion");
        for (int i = 0; i <= n; i++) {
            Console.Write(arr[i] + " ");
        }
    }
}
JavaScript
// JavaScript program to insert given element at the end
// of an array using custom methods

let n = 4;
let arr = [10, 20, 30, 40, 0];
let ele = 50;

console.log("Array before insertion");
console.log(arr.slice(0, n).join(" "));

// Inserting element at the end of the array
arr[n] = ele;

console.log("Array after insertion");
console.log(arr.join(" "));

Output
Array before insertion
10 20 30 40 
Array after insertion
10 20 30 40 50 

Time Complexity: O(1)
Auxiliary Space: O(1)


Next Article
Article Tags :
Practice Tags :

Similar Reads