An array is a fixed collection of items of the same data type represented by a single variable. We can access the sequential Data Structure item called ‘element’ using an array index. Arrays can be single dimensional or 2 dimensional with rows and columns.

Single Dimension Array

Multi-dimensional array

An array is used to implement other Data Structures such as Stacks, Queues and List.

How is an array represented?

You can define integer arrays, string arrays or object arrays. Array size has to be defined when it is declared. This ensures memory is allocated based on the array size. 

For ex: int myarray[5];

We can initialize an array either during declaration or later.

For ex: type myarray[size] = {values/elements}

Arrays can be declared in many ways depending on the programming language used. For example, string in C is represented by a character array. Here, we have declared and initialized a character array. 

char charArray = {‘H’,”E’,’L’,’L’,’O’}

We can declare an array of Strings in the following way:

String names[3] = {“John”,”Kelvin”,”Simmons”}

Array index [0]John 
Array index [1]Kelvin 
Array index [2]Simmon

s

Array Operations

An array supports the following basic operations:

  • Insertion – insert an array element at the index and ensure there is no overflow
  • Deletion – delete an array element at the index
  • Traverse – loops through and prints all the array elements sequentially
  • Search – search by using an array index or by the value
  • Update – update the array element of the index

Let’s see how each operation works.


Array Insertion and Traversal

You can insert one or more elements into an array, in the beginning, end or any index. Let’s see the steps of inserting an array element at index 3. 

int arr[5]={10,9,8,6}  insert 7 at arr[3]

Start traversing from the last position and move to the next until you reach the 3rd position. Remember, arr[0] is the 1st element. We need to reach arr[3] which has value 6 in our example. Move arr[3] value to the right and insert 7 in arr[3].

Array deletion and Traversal

Deletion at the beginning and end of the array is straight forward. However, when you have to delete from the middle of the array, rearrange the element to occupy the space emptied.

Let’s take the same example as above and delete the element with value 8 which is arr[2].

Traverse through arr[3] and delete the value of arr[3]. This value now becomes null. Go to the next index arr[4] and move this element to the 3rd position. Stop the move operation if this is the end of the array.

The new array now has these elements: {10,9,6}

Array search

We can search an array for a specific element using the index or value. When you traverse through an array in a loop iterators reference the element and help to move to the next position. 

Below is a pseudo code to indicate the same :

 

   for(iter=0;iter<n;iter++)

   {

   if ( arr[iter]== item)

   Return iter;

   }

The Interview questions for Arrays and other Data Structures and Algorithm can be found in our book at Vibrant Publishers

previous arrow
next arrow
Slider
Shilpa Hegde

Author: Shilpa Hegde

Shilpa Hegde is a Software Engineer by profession and has a passion for writing on any niche. She has been writing poems, articles, blogs, Press Releases, reviews, to name a few. Some of her poems have been published on Sentinal Poetry movement, Stellar showcase Journal, and Blue Fog Journal. Her articles are published on Vivo group, adideo.com, broadwayworld.com, 4fastplumber, longboard-brand, and many other places.