Blog_Banner_Asset
    Homebreadcumb forward arrow iconBlogbreadcumb forward arrow iconData Sciencebreadcumb forward arrow iconArray in Data Structure – Explanation, Function & Examples

Array in Data Structure – Explanation, Function & Examples

Last updated:
20th Jun, 2021
Views
Read Time
13 Mins
share image icon
In this article
Chevron in toc
View All
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. 

Learn Data Science Courses online at upGrad

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 LanguageDefined array contentsDefined 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];
Javaint[] marks = {10, 20, 30}; int[] marks = = new int[3];
JavaScriptvar marks = [10, 20, 30];var marks = new Array(3);
Pythonmarks = [10, 20, 30]marks = [None] * 3
Swiftvar 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 LanguageClassAddition of elementRemoval of element
C++#include <list>

std::list

inserterase
C#System.Collections.Generic.ListAddRemove
Javajava.util.ArrayListadd  remove
JavaScriptArraypush, splicepop, splice
PythonListappendremove
SwiftArrayappendremove

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.

Explore our Popular Data Science Courses

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. 

Explore our Popular Data Science Courses

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 

Top Data Science Skills to Learn

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

upGrad’s Exclusive Data Science Webinar for you –

Watch our Webinar on How to Build Digital & Data Mindset?

 

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

Advantages of array

  • 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.

Read our popular Data Science Articles

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.

Profile

Rohan Vats

Blog Author
Software Engineering Manager @ upGrad. Passionate about building large scale web apps with delightful experiences. In pursuit of transforming engineers into leaders.

Frequently Asked Questions (FAQs)

1What are the advantages and disadvantages of an array?

An array is a powerful linear data structure. However, it has some advantages as well as disadvantages that are mentioned below:
Advantages
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.
Disadvantages
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.

2Differentiate 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.

3Describe 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.

Explore Free Courses

Suggested Blogs

Top 13 Highest Paying Data Science Jobs in India [A Complete Report]
905272
In this article, you will learn about Top 13 Highest Paying Data Science Jobs in India. Take a glimpse below. Data Analyst Data Scientist Machine
Read More

by Rohit Sharma

12 Apr 2024

Most Common PySpark Interview Questions &#038; Answers [For Freshers &#038; Experienced]
20930
Attending a PySpark interview and wondering what are all the questions and discussions you will go through? Before attending a PySpark interview, it’s
Read More

by Rohit Sharma

05 Mar 2024

Data Science for Beginners: A Comprehensive Guide
5068
Data science is an important part of many industries today. Having worked as a data scientist for several years, I have witnessed the massive amounts
Read More

by Harish K

28 Feb 2024

6 Best Data Science Institutes in 2024 (Detailed Guide)
5179
Data science training is one of the most hyped skills in today’s world. Based on my experience as a data scientist, it’s evident that we are in
Read More

by Harish K

28 Feb 2024

Data Science Course Fees: The Roadmap to Your Analytics Career
5075
A data science course syllabus covers several basic and advanced concepts of statistics, data analytics, machine learning, and programming languages.
Read More

by Harish K

28 Feb 2024

Inheritance in Python | Python Inheritance [With Example]
17649
Python is one of the most popular programming languages. Despite a transition full of ups and downs from the Python 2 version to Python 3, the Object-
Read More

by Rohan Vats

27 Feb 2024

Data Mining Architecture: Components, Types &#038; Techniques
10805
Introduction Data mining is the process in which information that was previously unknown, which could be potentially very useful, is extracted from a
Read More

by Rohit Sharma

27 Feb 2024

6 Phases of Data Analytics Lifecycle Every Data Analyst Should Know About
80786
What is a Data Analytics Lifecycle? Data is crucial in today’s digital world. As it gets created, consumed, tested, processed, and reused, data goes
Read More

by Rohit Sharma

19 Feb 2024

Sorting in Data Structure: Categories &#038; Types [With Examples]
139145
The arrangement of data in a preferred order is called sorting in the data structure. By sorting data, it is easier to search through it quickly and e
Read More

by Rohit Sharma

19 Feb 2024

Schedule 1:1 free counsellingTalk to Career Expert
icon
footer sticky close icon