Each programming language has many data structures because they provide a basic block to a programming language. There are many kinds of data structures in a programming language that developers use often some of those are built-in data structures and some are user-defined.
What is a Data Structure?
In computer science when we deal with a large amount of data that relate somehow, the first question arises how to manage this data or how to organize this? Here data structures come in play.
A data structure is a technique or method to organize the data so that it could be efficiently managed and stored in the memory. Basically, a data structure is a collection of data types. Some of the famous data structures are arrays, linked list, structure, trees, etc.
What is an Array?
Arrays are the most famous data structures, if we want to define an array, we can say that an array is a data structure, which is a collection of homogeneous data elements, where data elements are data types values or variables.
It is an ordered collection of similar data types which use indexing and each element of the array stored in the contiguous memory location.
Declaration of an array:
int array; # array declaration
When to use an array?
When there are lots of variables in a program and somehow, they all related to each other and have same data type in this case instead of giving each value a variable name we would store all values inside an array. That’s how it will dissolve the problem of creating many variable names and give easy access over all the values.
int a, b,c,d,e,f,g,h; a=10; b=20; c=30; d=56; e=70; f=30; g=23; h=34;
Here in the above pseudocode we have declared 8 variables and assigned values to them, this approach of coding could be tedious, each developer should have knowledge of data structures and should know when to pick which Data structure.
The above problem could be solved with the help of an array.
int = array; array=10; array=20; …..
Here instead of giving different variable names to values, we store all values using one variable name array.
Array use indexing to store value, indexing in arrays starts with 0 and end with (n-1) where n is the total numbers of elements that can be stored in an array.
Indexing could also be used to retrieve values from an array.
Arrays are of three types:
- 1-D Array.
- 2-D Array
- Multi-dimensional Array.
A 1-D Array or single-dimensional Array is a collection of linear data structure where each array element is a primitive data type that can not be divided further.
int s_array s_array[1,2,3,4,5]
A 2-D Array is a 1-D array where each element itself an array. 2-D arrays can be represented as a Matrix.
int array_2D; array_2D [ [1,2,3], [1,2,3], [1,2,3] ];
A Multi-Dimensional Array is used when we want more than two, 2-D arrays inside a single array.
Some Advantages and Disadvantages of Arrays
- It is an efficient way to store Homogenous data
- By far it is the fastest data structure.
- It can store data in a contiguous memory location.
- Its index makes data retrieval very fast.
- You can store data in arrays in any Dimension
- We can store only fixed numbers of elements in an array
- Inserting a new element in the middle of an array is difficult and it affects other elements too.
Arrays if one of the most popular and basic Data structures. In technical interviews and competitive programming questions from arrays are frequent. There are many data structure that looks and works like an array but a programmer should have a good instinct when and where to use arrays.