Explore Courses
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Birla Institute of Management Technology Birla Institute of Management Technology Post Graduate Diploma in Management (BIMTECH)
  • 24 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Popular
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science & AI (Executive)
  • 12 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
University of MarylandIIIT BangalorePost Graduate Certificate in Data Science & AI (Executive)
  • 8-8.5 Months
upGradupGradData Science Bootcamp with AI
  • 6 months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
OP Jindal Global UniversityOP Jindal Global UniversityMaster of Design in User Experience Design
  • 12 Months
Popular
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Rushford, GenevaRushford Business SchoolDBA Doctorate in Technology (Computer Science)
  • 36 Months
IIIT BangaloreIIIT BangaloreCloud Computing and DevOps Program (Executive)
  • 8 Months
New
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Popular
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
Golden Gate University Golden Gate University Doctor of Business Administration in Digital Leadership
  • 36 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
Popular
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
Bestseller
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
IIIT BangaloreIIIT BangalorePost Graduate Certificate in Machine Learning & Deep Learning (Executive)
  • 8 Months
Bestseller
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in AI and Emerging Technologies (Blended Learning Program)
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
ESGCI, ParisESGCI, ParisDoctorate of Business Administration (DBA) from ESGCI, Paris
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration From Golden Gate University, San Francisco
  • 36 Months
Rushford Business SchoolRushford Business SchoolDoctor of Business Administration from Rushford Business School, Switzerland)
  • 36 Months
Edgewood CollegeEdgewood CollegeDoctorate of Business Administration from Edgewood College
  • 24 Months
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with Concentration in Generative AI
  • 36 Months
Golden Gate University Golden Gate University DBA in Digital Leadership from Golden Gate University, San Francisco
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Deakin Business School and Institute of Management Technology, GhaziabadDeakin Business School and IMT, GhaziabadMBA (Master of Business Administration)
  • 12 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science (Executive)
  • 12 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityO.P.Jindal Global University
  • 12 Months
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (AI/ML)
  • 36 Months
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDBA Specialisation in AI & ML
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
New
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGrad KnowledgeHutupGrad KnowledgeHutAzure Administrator Certification (AZ-104)
  • 24 Hours
KnowledgeHut upGradKnowledgeHut upGradAWS Cloud Practioner Essentials Certification
  • 1 Week
KnowledgeHut upGradKnowledgeHut upGradAzure Data Engineering Training (DP-203)
  • 1 Week
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
Loyola Institute of Business Administration (LIBA)Loyola Institute of Business Administration (LIBA)Executive PG Programme in Human Resource Management
  • 11 Months
Popular
Goa Institute of ManagementGoa Institute of ManagementExecutive PG Program in Healthcare Management
  • 11 Months
IMT GhaziabadIMT GhaziabadAdvanced General Management Program
  • 11 Months
Golden Gate UniversityGolden Gate UniversityProfessional Certificate in Global Business Management
  • 6-8 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
IU, GermanyIU, GermanyMaster of Business Administration (90 ECTS)
  • 18 Months
Bestseller
IU, GermanyIU, GermanyMaster in International Management (120 ECTS)
  • 24 Months
Popular
IU, GermanyIU, GermanyB.Sc. Computer Science (180 ECTS)
  • 36 Months
Clark UniversityClark UniversityMaster of Business Administration
  • 23 Months
New
Golden Gate UniversityGolden Gate UniversityMaster of Business Administration
  • 20 Months
Clark University, USClark University, USMS in Project Management
  • 20 Months
New
Edgewood CollegeEdgewood CollegeMaster of Business Administration
  • 23 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
KnowledgeHut upGradKnowledgeHut upGradBackend Development Bootcamp
  • Self-Paced
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 5 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
upGradupGradUI/UX Bootcamp
  • 3 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
upGradupGradDigital Marketing Accelerator Program
  • 05 Months

Array in Data Structure – Explanation, Function & Examples

Updated on 24 November, 2022

8.73K+ views
13 min read

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 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 <list>

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])

 

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

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.

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.

Frequently Asked Questions (FAQs)

1. What 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.

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

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