Operations on Array
1. Array Traversal:
Array traversal involves visiting all the elements of the array once. Below is the implementation of Array traversal in different Languages:
int arr[] = { 1, 2, 3, 4, 5 };
int len = sizeof(arr) / sizeof(arr[0]);
// Traversing over arr[]
for (int i = 0; i < len; i++) {
printf("%d ", arr[i]);
}
int arr[] = { 1, 2, 3, 4, 5 };
// Traversing over arr[]
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
import array
arr = array.array('i', [1, 2, 3, 4, 5])
# Traversing over arr[]
for x in arr:
print(x, end=" ")
int[] arr = { 1, 2, 3, 4, 5 };
// Traversing over arr[]
for (int i = 0; i < arr.Length; i++)
Console.Write(" " + arr[i]);
let arr = [1, 2, 3, 4, 5]
// Traversing over arr[]
for (let x of arr)
console.log(x)
int arr[] = { 1, 2, 3, 4, 5 };
int len = sizeof(arr) / sizeof(arr[0]);
// Traversing over arr[]
for (int i = 0; i < len; i++) {
cout << arr[i] << " ";
2. Insertion in Array:
We can insert one or multiple elements at any position in the array. Below is the implementation of Insertion in Array in different languages:
// Function to insert element
// at a specific position
void insertElement(int arr[], int n, int x, int pos)
{
// shift elements to the right
// which are on the right side of pos
for (int i = n - 1; i >= pos; i--)
arr[i + 1] = arr[i];
arr[pos] = x;
}
// Function to insert element
// at a specific position
void insertElement(int arr[], int n, int x, int pos)
{
// shift elements to the right
// which are on the right side of pos
for (int i = n - 1; i >= pos; i--)
arr[i + 1] = arr[i];
arr[pos] = x;
}
static void insertElement(int arr[], int n, int x, int pos)
{
// shift elements to the right
// which are on the right side of pos
for (int i = n - 1; i >= pos; i--)
arr[i + 1] = arr[i];
arr[pos] = x;
}
# python Program to Insert an element
# at a specific position in an Array
def insertElement(arr, n, x, pos):
# shift elements to the right
# which are on the right side of pos
for i in range(n-1, pos-1, -1):
arr[i + 1] = arr[i]
arr[pos] = x
static void insertElement(int[] arr, int n, int x, int pos)
{
// shift elements to the right
// which are on the right side of pos
for (int i = n - 1; i >= pos; i--)
arr[i + 1] = arr[i];
arr[pos] = x;
}
// javascript Program to Insert an element
// at a specific position in an Array
function insertElement(arr, n, x, pos)
{
// shift elements to the right
// which are on the right side of pos
var i = n - 1;
for (i; i >= pos; i--)
{
arr[i + 1] = arr[i];
}
arr[pos] = x;
}
Deletion in Array:
We can delete an element at any index in an array. Below is the implementation of Deletion of element in an array:
// To search a key to be deleted
int findElement(int arr[], int n, int key);
// Function to delete an element
int deleteElement(int arr[], int n, int key)
{
// Find position of element to be deleted
int pos = findElement(arr, n, key);
if (pos == -1) {
cout << "Element not found";
return n;
}
// Deleting element
int i;
for (i = pos; i < n - 1; i++)
arr[i] = arr[i + 1];
return n - 1;
}
// Function to implement search operation
int findElement(int arr[], int n, int key)
{
int i;
for (i = 0; i < n; i++)
if (arr[i] == key)
return i;
// Return -1 if key is not found
return -1;
}
#include <iostream>
using namespace std;
int main() {
cout << "GFG!";
return 0;
}
// C program to implement delete operation in a
// unsorted array
#include <stdio.h>
// To search a key to be deleted
int findElement(int arr[], int n, int key);
// Function to delete an element
int deleteElement(int arr[], int n, int key)
{
// Find position of element to be deleted
int pos = findElement(arr, n, key);
if (pos == -1) {
printf("Element not found");
return n;
}
// Deleting element
int i;
for (i = pos; i < n - 1; i++)
arr[i] = arr[i + 1];
return n - 1;
}
// Function to implement search operation
int findElement(int arr[], int n, int key)
{
int i;
for (i = 0; i < n; i++)
if (arr[i] == key)
return i;
// Return -1 if key is not found
return -1;
}
// function to search a key to
// be deleted
static int findElement(int arr[], int n, int key)
{
int i;
for (i = 0; i < n; i++)
if (arr[i] == key)
return i;
// Return -1 if key is not found
return -1;
}
// Function to delete an element
static int deleteElement(int arr[], int n, int key)
{
// Find position of element to be
// deleted
int pos = findElement(arr, n, key);
if (pos == -1) {
System.out.println("Element not found");
return n;
}
// Deleting element
int i;
for (i = pos; i < n - 1; i++)
arr[i] = arr[i + 1];
return n - 1;
}
from array import array
# Function to search for a key in the array
def findElement(arr, n, key):
for i in range(n):
# Return the index if key is found
if arr[i] == key:
return i
# Return -1 if key is not found
return -1
# Function to delete an element from the array
def deleteElement(arr, n, key):
# Find position of element to be deleted
pos = findElement(arr, n, key)
if pos == -1:
print("Element not found")
return n
# Deleting element
for i in range(pos, n - 1):
arr[i] = arr[i + 1]
return n - 1
int findElement(int[] arr, int n, int key)
{
int i;
for (i = 0; i < n; i++)
if (arr[i] == key)
return i;
return -1;
}
// Function to delete an element
int deleteElement(int[] arr, int n, int key)
{
// Find position of element
// to be deleted
int pos = findElement(arr, n, key);
if (pos == -1) {
Console.WriteLine("Element not found");
return n;
}
// Deleting element
int i;
for (i = pos; i < n - 1; i++)
arr[i] = arr[i + 1];
return n - 1;
}
// function to search a key to be deleted
function findElement(arr,n,key)
{
let i;
for (i = 0; i < n; i++)
if (arr[i] == key)
return i;
return -1;
}
// Function to delete an element
function deleteElement(arr,n,key)
{
// Find position of element to be deleted
let pos = findElement(arr, n, key);
if (pos == -1)
{
document.write("Element not found");
return n;
}
// Deleting element
let i;
for (i = pos; i< n - 1; i++)
arr[i] = arr[i + 1];
return n - 1;
}
Searching in Array:
We can traverse over an array and search for an element. Below is the implementation of Deletion of element in an array:
// Function to implement search operation
int findElement(int arr[], int n, int key)
{
int i;
for (i = 0; i < n; i++)
if (arr[i] == key)
return i;
// If the key is not found
return -1;
}
// Function to implement search operation
int findElement(int arr[], int n, int key)
{
int i;
for (i = 0; i < n; i++)
if (arr[i] == key)
return i;
// If the key is not found
return -1;
}
// Function to implement search operation
int findElement(int arr[], int n, int key)
{
for (int i = 0; i < n; i++)
if (arr[i] == key)
return i;
// If the key is not found
return -1;
}
# Python program for searching in
# unsorted array
def findElement(arr, n, key):
for i in range(n):
if (arr[i] == key):
return i
# If the key is not found
return -1
// Function to implement
// search operation
int findElement(int[] arr, int n, int key)
{
for (int i = 0; i < n; i++)
if (arr[i] == key)
return i;
// If the key is not found
return -1;
}
// Function to implement search operation
function findElement( arr, n, key)
{
let i;
for (i = 0; i < n; i++)
if (arr[i] == key)
return i;
return -1;
}
Getting Started with Array Data Structure
Array is a collection of items of the same variable type that are stored at contiguous memory locations. It is one of the most popular and simple data structures used in programming. In this article, we have decided to provide a complete guide for Arrays, which will help you to tackle any problem based on Arrays.
Table of Content
- What is an Array?
- Basic terminologies of array
- Memory representation of Array
- Declaration of Array
- Initialization of Array
- Importance of Array
- Types of arrays
- Operations on Array
- Complexity Analysis of Operations on Array
- Advantages of Array
- Disadvantages of Array
- Applications of Array
- Top theoretical interview questions
- Frequently Asked Questions (FAQs) on Arrays:
Contact Us