Introduction to Arrays - Data Structure and Algorithm Tutorials - GeeksforGeeks
Introduction to Arrays - Data Structure and Algorithm Tutorials - GeeksforGeeks
Pankaj Rawat
[email protected]
What is an Array?
An array is a collection of items of the same variable type stored that are stored
at contiguous memor y locations. It ’s one of the most popular and simple data
structures and is often used to implement other data structures. Each item in
The dream of ever y programmer is to become not just a good, but also a great
programmer. We all want to achieve our goals and to achieve our goals, we must have
a great plan with us. In this context, we have decided to provide a complete guide for
Arrays inter view preparation, which will help you to tackle the problems that are
mostly asked in the inter view, such as What is an Array, What is Array in C language,
How do you initialize an Array in C, How to sor t an Array, etc. We have also covered
the topic s such as Top Theoretical inter view questions and Top inter view coding
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge
that you have read and understood our Cookie Policy & Privacy Policy
Got It !
▲
https://www.geeksforgeeks.org/introduction-to-arrays-data-structure-and-algorithm-tutorials/ 1/20
3/7/23, 12:35 PM Introduction to Arrays - Data Structure and Algorithm Tutorials - GeeksforGeeks
Array Index : In an array, elements are identified by their indexes. Array index
star ts from 0.
Array element : Elements are items stored in an array and can be accessed by their
index.
can contain.
Representation of Array
LEARN MORE
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge
that you have read and understood our Cookie Policy & Privacy Policy
https://www.geeksforgeeks.org/introduction-to-arrays-data-structure-and-algorithm-tutorials/ 2/20
3/7/23, 12:35 PM Introduction to Arrays - Data Structure and Algorithm Tutorials - GeeksforGeeks
Array
Arrays can be declared in various ways in different languages. For better illustration,
C++
Java
// Example:
int arr[] = { 2, 5, 6, 9, 7, 4, 3 };
C#
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge
int[] arr = new that int[5]; // This array will store integer type element
you have read and understood our Cookie Policy & Privacy Policy
https://www.geeksforgeeks.org/introduction-to-arrays-data-structure-and-algorithm-tutorials/ 3/20
3/7/23, 12:35 PM Introduction to Arrays - Data Structure and Algorithm Tutorials - GeeksforGeeks
// JS code
let arr=[10,20,30]; // This array will store integer
let arr2 = ['c', 'd', 'e'] // This array will store characters
let arr3 = [28.5, 36.5, 40.2] // This array will store floating elements
P ython
# Python code
arr = [10, 20, 30] # This array will store integer
arr2 = ['c', 'd', 'e'] # This array will store characters
arr3 = [28.5, 36.5, 40.2] # This array will store floating elements
Array declaration
means that the array element ’s memor y is allocated when a program is compiled.
Here only a fixed size (i,e. the size that is mentioned in square brackets []) of memor y
will be allocated for storage, but don’t you think it will not be the same situation as we
know the size of the array ever y time, there might be a case where we don’t know the
size of the array. If we declare a larger size and store a lesser number of elements will
result in a wastage of memor y or either be a case where we declare a lesser size then
we won’t get enough memor y to store the rest of the elements. In such cases, static
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge
that you have read and understood our Cookie Policy & Privacy Policy
https://www.geeksforgeeks.org/introduction-to-arrays-data-structure-and-algorithm-tutorials/ 4/20
3/7/23, 12:35 PM Introduction to Arrays - Data Structure and Algorithm Tutorials - GeeksforGeeks
memor y allocation is the process of assigning the memor y space during the
C++
Java
P ython3
# list of integers
my_list = [1, 2, 3, 4]
# Empty list
my_list = []
C#
int[]
We numArray
use cookies = new
to ensure int[]
you have {};
the best browsing experience on our website. By using our site, you acknowledge
that you have read and understood our Cookie Policy & Privacy Policy
https://www.geeksforgeeks.org/introduction-to-arrays-data-structure-and-algorithm-tutorials/ 5/20
3/7/23, 12:35 PM Introduction to Arrays - Data Structure and Algorithm Tutorials - GeeksforGeeks
GfG Job Fair - 2023 DSA Array Matrix Strings Hashing Linked List Stack Queue B
// JavaScript has dynamic arrays: their size is not predetermined, nor the type o
PHP
$arr = array("a","b","c");
A ssume there is a class of five students and if we have to keep records of their marks
in examination then, we can do this by declaring five variables individual and keeping
track of records but what if the number of students becomes ver y large, it would be
What it means is that, we can use normal variables (v1, v2, v3, ..) when we have a
becomes difficult to manage them with normal variables. The idea of an array is to
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge
that you have read and understood our Cookie Policy & Privacy Policy
https://www.geeksforgeeks.org/introduction-to-arrays-data-structure-and-algorithm-tutorials/ 6/20
3/7/23, 12:35 PM Introduction to Arrays - Data Structure and Algorithm Tutorials - GeeksforGeeks
Types of arrays :
One-dimensional array (1-D arrays): You can imagine a 1d array as a row, where
1D array
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge
that you have read and understood our Cookie Policy & Privacy Policy
https://www.geeksforgeeks.org/introduction-to-arrays-data-structure-and-algorithm-tutorials/ 7/20
3/7/23, 12:35 PM Introduction to Arrays - Data Structure and Algorithm Tutorials - GeeksforGeeks
2D array
3D array
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge
Sor ting: Maintaining the order of elements in the array.
that you have read and understood our Cookie Policy & Privacy Policy
https://www.geeksforgeeks.org/introduction-to-arrays-data-structure-and-algorithm-tutorials/ 8/20
3/7/23, 12:35 PM Introduction to Arrays - Data Structure and Algorithm Tutorials - GeeksforGeeks
position faster.
Arrays have better cache locality which makes a pretty big difference in
per formance.
Arrays represent multiple data items of the same type using a single name.
Arrays store multiple data of similar types with the same name.
Array data structures are used to implement the other data structures like linked
Disadvantages of Array:
A s arrays have a fixed size, once the memor y is allocated to them, it cannot be
Arrays store data in contiguous memor y locations, which makes deletion and
Application of Array:
They are used in the implementation of other data structures such as array lists,
It is used for different sor ting algorithms such as bubble sor t inser tion sor t, merge
https://www.geeksforgeeks.org/introduction-to-arrays-data-structure-and-algorithm-tutorials/ 9/20
3/7/23, 12:35 PM Introduction to Arrays - Data Structure and Algorithm Tutorials - GeeksforGeeks
6 “ What are the advantages of a linked list over an array? In which View
scenarios do
9 What are the benefits of Heap over Sor ted Arrays? View
strArray.length()?
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge
that you have read and understood our Cookie Policy & Privacy Policy
https://www.geeksforgeeks.org/introduction-to-arrays-data-structure-and-algorithm-tutorials/ 10/20
3/7/23, 12:35 PM Introduction to Arrays - Data Structure and Algorithm Tutorials - GeeksforGeeks
array?
27 How to merge two sor ted Arrays into a Sor ted Array? View
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge
that you have read and understood our Cookie Policy & Privacy Policy
34 Print all subarrays with 0 sum View
https://www.geeksforgeeks.org/introduction-to-arrays-data-structure-and-algorithm-tutorials/ 11/20
3/7/23, 12:35 PM Introduction to Arrays - Data Structure and Algorithm Tutorials - GeeksforGeeks
5 Find the Kth largest and Kth smallest number in an array View Solve
9 Move all the negative elements to one side of the array View Solve
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge
10 Find the Union and Intersection of the two sor ted arrays View Solve
that you have read and understood our Cookie Policy & Privacy Policy
https://www.geeksforgeeks.org/introduction-to-arrays-data-structure-and-algorithm-tutorials/ 12/20
3/7/23, 12:35 PM Introduction to Arrays - Data Structure and Algorithm Tutorials - GeeksforGeeks
integers
items
11 Find if there is any subarray with a sum equal to zero View Solve
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge
15 Find the longest consecutive subsequence View Solve
that you have read and understood our Cookie Policy & Privacy Policy
https://www.geeksforgeeks.org/introduction-to-arrays-data-structure-and-algorithm-tutorials/ 13/20
3/7/23, 12:35 PM Introduction to Arrays - Data Structure and Algorithm Tutorials - GeeksforGeeks
16 Find the minimum element in a rotated and sor ted array View Solve
Hard Problems
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge
that you have read and understood our Cookie Policy & Privacy Policy
https://www.geeksforgeeks.org/introduction-to-arrays-data-structure-and-algorithm-tutorials/ 14/20
3/7/23, 12:35 PM Introduction to Arrays - Data Structure and Algorithm Tutorials - GeeksforGeeks
5 Pair with the given sum in a sor ted array View Solve
An array is a collection of items of the same data type stored at contiguous memor y
Arrays store elements of the same type, they are classified as homogeneous data
structures. They can store numbers, strings, characters, boolean values (true and
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge
that you have read and understood our Cookie Policy & Privacy Policy
false), objects, and so on.
https://www.geeksforgeeks.org/introduction-to-arrays-data-structure-and-algorithm-tutorials/ 15/20
3/7/23, 12:35 PM Introduction to Arrays - Data Structure and Algorithm Tutorials - GeeksforGeeks
An array is a linear data structure that stores similar elements in contiguous memor y
locations.
Multidimensional array
An array is a collection of items of the same data type stored at contiguous memor y
locations or says the elements are stored one af ter another in memor y. An array uses
an index system star ting at 0 and going to (n-1), where n is its size.
The structure can contain variables of different types but an array only contains
An array is a collection of items of the same data type, That means, in an integer array
only integer values can be stored, while in a float array only floating values and
character array can have only characters. Thus, no array can have values of two data
types.
There are multiple advantages of array data structure and some of them are:
position faster.
We Arrays
use cookies to ensure
store you havedata
multiple the best
ofbrowsing
similar experience on ourthe
types with website.
sameBy using
name. our site, you acknowledge
that you have read and understood our Cookie Policy & Privacy Policy
Array data structures are used to implement the other data structures like linked
https://www.geeksforgeeks.org/introduction-to-arrays-data-structure-and-algorithm-tutorials/ 16/20
3/7/23, 12:35 PM Introduction to Arrays - Data Structure and Algorithm Tutorials - GeeksforGeeks
An array is used when several variables of the same type need to be used, and it can
row-major order.
Conclusion
Af ter the discussion, we concluded that arrays are a simple method of accessing
elements of the same type by grouping them and we can find the elements efficiently
by their indexes and can per form different operations using them. Thus, they are
more efficient when it comes to memor y allocation and should be used in all modern
programming languages. So, this becomes a favorite topic for the perspective of the
inter view and most of the companies generally asked about the problems on the
array. For all these reasons, we must have a good knowledge of it.
Related ar ticles :
How can one become good at Data Structures and Algorithms easily?
Amazon SDE Sheet – A Guide for Amazon SDE Inter view Preparation
Google Inter view Preparation For Sof tware Engineer – A Complete Guide
Related Articles
Start
3. Your Coding
Introduction Journey
to Strings Now! and Algorithm Tutorials
- Data Structure
Like 22
Previous Next
Ar ticle Contributed By :
GeeksforGeeks
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge
that you have read and understood our Cookie Policy & Privacy Policy
Easy Normal Medium Hard Expert
https://www.geeksforgeeks.org/introduction-to-arrays-data-structure-and-algorithm-tutorials/ 18/20
3/7/23, 12:35 PM Introduction to Arrays - Data Structure and Algorithm Tutorials - GeeksforGeeks
Company Learn
About Us DSA
Careers Algorithms
In Media Data Structures
Contact Us SDE Cheat Sheet
Privacy Policy Machine learning
Copyright Policy CS Subjects
Advertise with us Video Tutorials
Courses
News Languages
Top News
Python
Technology
Java
Work & Career
CPP
Business
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge
Golang
that you have read and understood our Cookie Policy & Privacy Policy
Finance
C#
Lifestyle
https://www.geeksforgeeks.org/introduction-to-arrays-data-structure-and-algorithm-tutorials/ 19/20
3/7/23, 12:35 PM Introduction to Arrays - Data Structure and Algorithm Tutorials - GeeksforGeeks
Knowledge SQL
Start Your Coding Journey Now! Kotlin
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge
that you have read and understood our Cookie Policy & Privacy Policy
https://www.geeksforgeeks.org/introduction-to-arrays-data-structure-and-algorithm-tutorials/ 20/20