Array in Data Structure – Explanation, Function & Examples

Data structures have proven to be a crucial part of almost all the programming languages that have been applied widely to most of the computer programs. It’s through the data structures that the data can be effectively managed and accessed by the programs as accessing and storing single data separately is a time-consuming process. Algorithms are designed specially to create specific operations needed in a data structure. Therefore, data structures and algorithms together lay the foundation of complex applications and programs.

In this article, we will be focusing on a type of data structure, i.e. Array.

An array is a type of data structure where the elements or the data is stored at contiguous locations. Whenever the user has a set of data with the same data type, the array data structure is the option for organizing that data. The size of an array depends on the size of the data. Before the elements are stored in an array, the size of the array has to be defined so that it incorporates all the elements effectively. Each element that is stored in an array has an index value assigned to it which helps in identifying the location of that element in the array. The first element of the array has an index value of zero.

The important terms associated with an array data structure are:

• Element: Element represents every object or item stored in the data structure.
• Index: Index represents the location of the element in an array. It has a numerical value.

The size of an array changes with different programming languages. Based on the size, an array may be of two types: a static and a dynamic array.

1. Static array:

These types of arrays have their sizes predefined during their creation. Because of this, the static arrays are also known as the fixed arrays or fixed-length arrays. The array may be defined in two ways. Either the elements of the array can be defined while creating the array or the size of the array can be defined while creating an array. In the latter case, the elements are not required to be specified. Default values may be allocated to an uninitialized array or values left in memory from previous allocations.

The array cannot shrink or expand once the size is defined. As memory gets allocated during the declaration of an array, it’s only the compiler that can destroy the array. The addition of an element is not possible as the user is not sure whether any free memory is present to allocate to the next element.

The below table shows the example of arrays used in different programming languages.

 Programming Language Defined array contents Defined size of array without contents C++ int marks[] = {10, 20, 30}; int marks[3]; C# int[] marks = {10, 20, 30}; int[] marks = = new int[3]; Java int[] marks = {10, 20, 30}; int[] marks = = new int[3]; JavaScript var marks = [10, 20, 30]; var marks = new Array(3); Python marks = [10, 20, 30] marks = [None] * 3 Swift var values:[Int] = [10, 20, 30] var marks: [Int] = [Int](repeating: 0, count: 3)

2. Dynamic Array

As the name suggests the array is dynamic which means that the elements can be added or it can be removed during run time. Compared to the static arrays whose length is fixed, the dynamic arrays don’t have any fixed length or size of the array. Standard library functions or built-in functions are available in most programming languages to create and manage dynamic arrays.

The below table shows the creation of an array in different programming languages

 Programming Language Class Addition of element Removal of element C++ #include std::list insert erase C# System.Collections.Generic.List Add Remove Java java.util.ArrayList add remove JavaScript Array push, splice pop, splice Python List append remove Swift Array append remove

Representation of an array

The representation of an array varies according to its implementation in different programming languages. The array being an important part of the python data structure, an illustration has been shown in the python programming language.

In python data structure arrays are handled through the keyword array. Whenever the keyword array is used the user has to store elements of the same data types.

Source

Figure 1: An example of an array

As per Figure 1, the illustration of the array shows that

• The size of the array is 10 which means that 9 elements can be stored in the array.
• The index value has been mentioned above the array which starts with the value of 0.
• The elements stored in the array can be any data type, and the element can be accessed through their index value.

Another illustration has been shown in Figure 2, where syntax of python and C++ has been described.

Source

Figure 2: Array declaration using python and C++ (

Properties of an Array

An array data structure has several properties:

• The elements stored within an array have the same data types and the same size, i.e. data type of int will have size of 4 bytes.
•  The contiguous memory location is used for storing the elements of a data structure. The smallest memory is allocated to the first element in the array.
• Index values are used to find the location of the elements in an array. The index starts with 0 and is always less than the total number of elements in the array.
• Random access of the elements in the array is possible due to the available index value. The address of the element can be calculated through the base address added to an offset value.
• The concept of the array remains the same in all the programming languages. Only the initialization and declaration vary.
• Array name, elements, and data type are the three parts that will be common in all languages.

Creating an array

The creation of an array in python data structure has been shown below.

• The array module in the python data structure can be imported for creating an array.
• array(data_type, value_list) is the syntax through which an array can be created in the python data structure.
• The data type should be real integers or floats. Strings are not allowed in python.

Figure 2 shows how to create an array in python. An example of a code to show how an array module is imported in python

import array

marks = array.array(‘i’, [100,200,300])

print(marks)

The declaration of an array can be carried out through

arrayName = array.array(type code for data type, [array,items])

This can be represented in Figure 3

Source

Figure 3: Array declaration in python

Important terms used in creating an array:

• Identifier: A name that has to be specified like a name for variables
• Module: special module called array has to be imported in python.
• Method: It is a specific method to initialize an array in python. Two arguments ate taken, typecode, and elements.
• Type Code: The data type has to be specified with the available type code.
• Elements: The array elements have to be specified within the square brackets, for example [200,400,100.]

The type code available are shown below

Array Operations

With the availability of data structures and algorithms, several operations can be carried out in any type of data structure. An array data structure can have operations like addition, deletion, accession, and updating an element.

The operations that can be carried out in an array of the python data structure are listed below.

1. Addition of an element to an array

• The built-in insert() function is used for adding elements to an array.
• Syntax used: arrayName.insert(index, value)
• Either one or more than one element can be added to the array through the insert() function.
• The elements can be added to the beginning of the array or at any specific position using the Input: append() function.

import array

marks = array.array(‘i’, [200,500,600])

marks.insert(1, 150)

Output: array(‘i’, [200,150,500,600])

An example with a code is shown below taken from

Output of the code:

Source

2. Deletion of an element in an array

• An element can be deleted from the array through its value.
• Syntax used: arrayName.remove(value)
• Example: removing the value of 250 after its addition in the array having elements 100, 300, 200, 500, and 800.

Input:

import array

marks = array.array(‘i’, [100,300,200,500,800])

marks.insert(1, 250)

print(marks)

marks.remove(250)

Output: array(‘i’, [100,300,200,500,800])

An example of a code taken from

Source

Output of the code:

3. Accessing elements in an array

• index operator [ ] is used for accessing elements in an array.
• The index number is used to access any element in the array.

An example of a code is shown below taken from

Output of the code:

Source

4. Searching element in an array.

• In-built index() method is used for searching an element in an array.
• The index value of the element to be searched is returned by the function.
• Example: searching for an element 250 in the array of elements 100, 250, 300, 200, 500, and 800.

Input: import array

marks = array.array(‘I’, [100,250,300,200,500,800])

print(marks.index(250))

Output: 1

A code for searching an element in an array

The output of the code is

Source

3. Updating elements in an array

• The process of updating an element is similar to the insertion method with the only difference that while updating the existing value will be replaced at the given index.
• The new value is reassigned to the index for updating the element in an array.
• Example: Updating an element 250 with 350 in the array of elements 100, 250, 300, 200, 500, and 800.

Input: import array

marks = array.array(‘i’, [100,250,300,200,500,800])

marks[1] = 350

Output:

array(‘i’, [100,350,300,200,500,800])

A code showing the updation of an element is shown below

The output of the code is

Source

• Storage of multiple values in a single variable is possible instead of creating separate variables for each element.
• Multiple values can be processed easily and quickly with the use of arrays.
• The elements of the array can be sorted and searched in a faster way.

Conclusion

The article discussed a special type of data structure i.e. the array and its associated operations. With the basic concepts, more complex programs could be built up targeting real-life problems. If you want to strengthen the foundations of your data structure concepts in python, you can refer to the following course of Executive PG Programme in Data Science by upGrad. The course is certified by IIIT-Bangalore and has over 14+ programming tools and languages to prepare your journey towards the industry. It is specially designed for entry level professionals within the age group of 21 to 45 years of age. So, don’t stop your learning here, and get hold of the language and its application in the world of machine learning through the course of upGrad. In case you have any queries, our team of assistance will be there to help you.

An array is a powerful linear data structure. However, it has some advantages as well as disadvantages that are mentioned below:
1. In an array, elements can be accessed easily by their index numbers.
2. Arrays can be used to store multiple similar entities.
3. The search operation is quite convenient. It can be done in O(n) time and O(log n) in a sorted array, where n is the number of elements.
1. Since the memory is statically allocated in an array, the size of the array can not be altered.
2. It is homogenous i.e., only the elements having a similar data type can be stored in an array.

Differentiate between an array and a list?

The following illustrates the difference between an array and a list.
Array -
1. The array data structure is homogenous i.e., only the elements with similar data types can be stored in an array.
2. Modules need to be imported before using the array.
3. Arithmetic operations are directly applicable.
4. Preferred for larger data.
4. Much more compact and consumes less memory.
List -
1. The list is heterogeneous and can store elements of multiple data types inside it.
2. No need to import modules as it is inbuilt in Python.
3. Arithmetic operations can not be operated directly.
4. Preferred for smaller data.
5. Memory consumption is more.

Describe major applications of arrays?

The array data structure has many applications in real life and it is also used as a base for implementing other user-defined data structures. Some of the major applications of arrays are as follows:
1. Arrays are used to implement and perform matrix operations. Matrices are largely used in geological surveys and science & research experiments.
2. Several user-defined data structures are implemented using the array data structures. These include stack, queue, heaps, hash tables, and lists.
3. Programs use arrays to regulate the control flow instead of using the traditional elif statements, which are lengthy comparatively.
4. Algorithms written for the CPU scheduling processes also use the array data structure to enhance CPU performance.
5. Graphs use the adjacency lists as one of their implementations. Vectors (application of array) are used to create these adjacency lists.

Prepare for a Career of the Future

UPGRAD AND IIIT-BANGALORE'S EXECUTIVE PG IN DATA SCIENCE
EXECUTIVE PG PROGRAM IN DATA SCIENCE

×