Difference between Array and Map
Last Updated :
18 Sep, 2023
Array:
An array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of the same type together. This makes it easier to calculate the position of each element by simply adding an offset to a base value, i.e., the memory location of the first element of the array (generally denoted by the name of the array).
The diagrammatic representation of the Array is given below:
Program 1:
Below is an illustration of a 1D array:
Output:
6 10 5 0
Program 2:
Below is an illustration of a 2D array:
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
int x[3][2] = { { 0, 1 }, { 2, 3 }, { 4, 5 } };
for ( int i = 0; i < 3; i++) {
for ( int j = 0; j < 2; j++) {
cout << "Element at x[" << i
<< "][" << j
<< "]: " ;
cout << x[i][j] << endl;
}
}
return 0;
}
|
Java
import java.util.*;
class GFG{
public static void main(String[] args)
{
int x[][] = { { 0 , 1 }, { 2 , 3 }, { 4 , 5 } };
for ( int i = 0 ; i < 3 ; i++)
{
for ( int j = 0 ; j < 2 ; j++)
{
System.out.print( "Element at x[" + i +
"][" + j + "]: " );
System.out.print(x[i][j] + "\n" );
}
}
}
}
|
Python3
if __name__ = = '__main__' :
x = [[ 0 , 1 ], [ 2 , 3 ], [ 4 , 5 ]];
for i in range ( 3 ):
for j in range ( 2 ):
print ( "Element at x[" , i ,
"][" , j , "]: " , end = "");
print (x[i][j]);
|
C#
using System;
class GFG{
public static void Main(String[] args)
{
int [,]x = { { 0, 1 }, { 2, 3 }, { 4, 5 } };
for ( int i = 0; i < 3; i++)
{
for ( int j = 0; j < 2; j++)
{
Console.Write( "Element at x[" + i +
"," + j + "]: " );
Console.Write(x[i,j] + "\n" );
}
}
}
}
|
Javascript
let x = [ [ 0, 1 ], [ 2, 3 ], [ 4, 5 ] ];
for (let i = 0; i < 3; i++)
{
for (let j = 0; j < 2; j++)
{
process.stdout.write( "Element at x[" + i +
"][" + j + "]: " );
console.log(x[i][j]);
}
}
|
Output
Element at x[0][0]: 0
Element at x[0][1]: 1
Element at x[1][0]: 2
Element at x[1][1]: 3
Element at x[2][0]: 4
Element at x[2][1]: 5
Map:
A map is an associative container that stores elements in a mapped fashion. Each element has a key value and a mapped value. No two mapped values can have equal key values.
The diagrammatic representation of Map is given below:
Program 1:
Below is an illustration of a map:
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
map< int , int > gquiz1;
gquiz1.insert(pair< int , int >(1, 40));
gquiz1.insert(pair< int , int >(2, 30));
gquiz1.insert(pair< int , int >(3, 60));
map< int , int >::iterator itr;
cout << "\nThe map gquiz1 is : \n" ;
cout << "\tKEY\tELEMENT\n" ;
for (itr = gquiz1.begin();
itr != gquiz1.end(); ++itr) {
cout << '\t' << itr->first
<< '\t' << itr->second
<< '\n' ;
}
return 0;
}
|
Java
import java.util.*;
class GFG{
public static void main(String[] args)
{
HashMap<Integer,
Integer> gquiz1 = new HashMap<Integer,
Integer>();
gquiz1.put( 1 , 40 );
gquiz1.put( 2 , 30 );
gquiz1.put( 3 , 60 );
Iterator<Map.Entry<Integer,
Integer>> itr = gquiz1.entrySet().
iterator();
System.out.print( "\nThe map gquiz1 is : \n" );
System.out.print( "KEY\tELEMENT\n" );
while (itr.hasNext())
{
Map.Entry<Integer,
Integer> entry = itr.next();
System.out.print( '\t' + entry.getKey()
+ "\t" + entry.getValue()+ "\n" );
}
}
}
|
Python3
if __name__ = = '__main__' :
gquiz1 = dict ()
gquiz1[ 1 ] = 40
gquiz1[ 2 ] = 30
gquiz1[ 3 ] = 60
print ( "\nThe map gquiz1 is : " )
print ( "KEY\tELEMENT" )
for x, y in gquiz1.items():
print (x, "\t" , y)
|
C#
using System;
using System.Collections.Generic;
class GFG{
public static void Main(String[] args)
{
Dictionary< int ,
int > gquiz1 = new Dictionary< int ,
int >();
gquiz1.Add(1, 40);
gquiz1.Add(2, 30);
gquiz1.Add(3, 60);
Console.Write( "\nThe map gquiz1 is : \n" );
Console.Write( "\tKEY\tELEMENT\n" );
foreach (KeyValuePair< int ,
int > entry in gquiz1)
{
Console.Write( "\t" + entry.Key +
"\t" + entry.Value + "\n" );
}
}
}
|
Javascript
let gquiz1 = new Map();
gquiz1.set(1, 40);
gquiz1.set(2, 30);
gquiz1.set(3, 60);
let itr;
console.log( "\nThe map gquiz1 is : \n" );
console.log( "\tKEY\tELEMENT\n" );
for (itr of gquiz1.entries()) {
console.log(`\t${itr[0]}\t${itr[1]}\n`);
}
|
Output
The map gquiz1 is :
KEY ELEMENT
1 40
2 30
3 60
Program 2:
Below is an illustration of an unordered map:
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
unordered_map<string, int > umap;
umap[ "GeeksforGeeks" ] = 10;
umap[ "Practice" ] = 20;
umap[ "Contribute" ] = 30;
for ( auto x : umap)
cout << x.first << " "
<< x.second << endl;
return 0;
}
|
Java
import java.util.*;
class GFG{
public static void main(String[] args)
{
HashMap<String,
Integer> umap = new HashMap<>();
umap.put( "GeeksforGeeks" , 10 );
umap.put( "Practice" , 20 );
umap.put( "Contribute" , 30 );
for (Map.Entry<String,
Integer> x : umap.entrySet())
System.out.print(x.getKey() + " " +
x.getValue() + "\n" );
}
}
|
Python3
class GFG :
@staticmethod
def main( args) :
umap = dict ()
umap[ "GeeksforGeeks" ] = 10
umap[ "Practice" ] = 20
umap[ "Contribute" ] = 30
for key,value in umap.items() :
print (key + " " + str (umap.get(key)) + "\n" , end = "")
if __name__ = = "__main__" :
GFG.main([])
|
C#
using System;
using System.Collections.Generic;
class GFG{
public static void Main(String[] args)
{
Dictionary<String, int > umap = new Dictionary<String,
int >();
umap.Add( "Contribute" , 30);
umap.Add( "GeeksforGeeks" , 10);
umap.Add( "Practice" , 20);
foreach (KeyValuePair<String, int > x in umap)
Console.Write(x.Key + " " + x.Value + "\n" );
}
}
|
Javascript
let umap = new Map();
umap.set( "GeeksforGeeks" , 10);
umap.set( "Practice" , 20);
umap.set( "Contribute" , 30);
for (let [key, value] of umap) {
console.log(key + " " + value);
}
|
Output
Contribute 30
GeeksforGeeks 10
Practice 20
Program 3:
Below is an illustration of a multimap:
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
multimap< int , int > gquiz1;
gquiz1.insert(pair< int , int >(1, 40));
gquiz1.insert(pair< int , int >(2, 30));
multimap< int , int >::iterator itr;
cout << "\nThe multimap gquiz1 is : \n" ;
cout << "\tKEY\tELEMENT\n" ;
for (itr = gquiz1.begin();
itr != gquiz1.end(); ++itr) {
cout << '\t' << itr->first
<< '\t' << itr->second
<< '\n' ;
}
return 0;
}
|
Java
import java.util.*;
public class Main {
public static void main(String[] args) {
Map<Integer, Integer> gquiz1 = new TreeMap<>();
gquiz1.put( 1 , 40 );
gquiz1.put( 2 , 30 );
Iterator<Map.Entry<Integer, Integer>> itr = gquiz1.entrySet().iterator();
System.out.println( "The multimap gquiz1 is : " );
System.out.println( "\tKEY\tELEMENT" );
while (itr.hasNext()) {
Map.Entry<Integer, Integer> entry = itr.next();
System.out.println( "\t" + entry.getKey() + "\t" + entry.getValue());
}
}
}
|
Python3
gquiz1 = {}
gquiz1[ 1 ] = 40
gquiz1[ 2 ] = 30
itr = gquiz1.items()
print ( "\nThe multimap gquiz1 is : " )
print ( "\tKEY\tELEMENT" )
for (key, value) in itr:
print ( '\t' , key, '\t' , value)
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
public static void Main()
{
SortedDictionary< int , int > gquiz1
= new SortedDictionary< int , int >();
gquiz1.Add(1, 40);
gquiz1.Add(2, 30);
SortedDictionary< int , int >.Enumerator itr;
Console.WriteLine( "\nThe multimap gquiz1 is : " );
Console.WriteLine( "\tKEY\tELEMENT" );
for (itr = gquiz1.GetEnumerator();
itr.MoveNext();) {
Console.WriteLine( "\t{0}\t{1}" , itr.Current.Key,
itr.Current.Value);
}
}
}
|
Javascript
let gquiz1 = {};
gquiz1[1] = 40;
gquiz1[2] = 30;
let itr = Object.entries(gquiz1);
console.log( "\nThe multimap gquiz1 is : " );
console.log( "\tKEY\tELEMENT" );
for (let [key, value] of itr) {
console.log( '\t' , key, '\t' , value);
}
|
Output
The multimap gquiz1 is :
KEY ELEMENT
1 40
2 30
Program 4:
Below is an illustration of an unordered multimap:
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
unordered_multimap<string, int > umm1;
unordered_multimap<string, int > umm2(
{ { "apple" , 1 },
{ "ball" , 2 },
{ "apple" , 10 },
{ "cat" , 7 },
{ "dog" , 9 },
{ "cat" , 6 },
{ "apple" , 1 } });
for ( auto x : umm2) {
cout << x.first << " "
<< x.second << endl;
}
return 0;
}
|
Java
import java.util.*;
public class Main {
public static void main(String[] args)
{
HashMap<String, ArrayList<Integer> >
umm2 = new HashMap<>() {
{
put( "apple" ,
new ArrayList<>(
Arrays.asList( 1 , 10 , 1 )));
put( "ball" ,
new ArrayList<>(
Collections.singletonList( 2 )));
put( "cat" , new ArrayList<>(
Arrays.asList( 7 , 6 )));
put( "dog" ,
new ArrayList<>(
Collections.singletonList( 9 )));
}
};
for (Map.Entry<String, ArrayList<Integer> > entry :
umm2.entrySet()) {
String key = entry.getKey();
ArrayList<Integer> values = entry.getValue();
for ( int value : values) {
System.out.println(key + " " + value);
}
}
}
}
|
Python3
from collections import defaultdict
GFG = defaultdict( list )
GFG[ "apple" ] = [ 1 , 10 , 1 ]
GFG[ "ball" ] = [ 2 ]
GFG[ "cat" ] = [ 7 , 6 ]
GFG[ "dog" ] = [ 9 ]
for key, values in GFG.items():
for value in values:
print (key, value)
|
C#
using System;
using System.Collections.Generic;
class Program {
static void Main( string [] args)
{
var umm2
= new SortedDictionary< string , List< int > >() {
{ "apple" , new List< int >(){ 1, 10, 1 } },
{ "ball" , new List< int >(){ 2 } },
{ "cat" , new List< int >(){ 7, 6 } },
{
"dog" , new List< int >() { 9 }
}
};
foreach (
KeyValuePair< string , List< int > > entry in umm2)
{
string key = entry.Key;
List< int > values = entry.Value;
foreach ( int value in values)
{
Console.WriteLine(key + " " + value);
}
}
}
}
|
Javascript
const umm2 = new Map([
[ "apple" , [1, 10, 1]],
[ "ball" , [2]],
[ "cat" , [7, 6]],
[ "dog" , [9]]
]);
for (let [key, values] of umm2) {
values.forEach(value => {
console.log(key + " " + value);
});
}
|
Output
apple 1
apple 10
apple 1
ball 2
cat 6
cat 7
dog 9
Difference between Array and Map
An Array is a collection of elements of the same data type. |
The map is a hashed structure of key and value pairs. |
The indices of the list are integers starting from 0. |
The keys of the Map can be of any data type. |
The elements are accessed via indices. |
The elements are accessed via key-values. |
The order of the elements entered is maintained. |
There is no guarantee for maintaining order. |
The array can be 1D, 2D or multidimensional |
Maps can be multimap, Unordered Multimap, Unordered map, etc |
Array’s size must be specified during the array declaration. |
The map’s size is dynamic. |
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...