Unveiling the Distinctions: Jagged Arrays Versus Multidimensional Arrays

Firat Tonak
4 min readJan 22, 2023

--

Today, we will delve into the disparities between a Jagged Array and a Multidimensional Array. It is imperative to elucidate these distinctions as encountering them in questions can be confounding. Following our comprehensive understanding of the variances and their practical implementation, we will then proceed to tackle a LeetCode question intricately linked to this topic.

Jagged Array

A jagged array is a construct wherein arrays are nested within an array variable, denoted by [][]. The initial bracket designates the quantity of arrays, while the subsequent bracket delineates the dimensionality of each array. This arrangement allows for a flexible and dynamic structure, offering a nuanced approach to array organization.

Accessing the arrays is achieved through the utilization of a straightforward for loop.

for (int i = 0; i < jaggedArray.Length; i++)// access the array part
{
for (int k = 0; k < jaggedArray[i].Length; k++)// access the dimension part
{
Console.Write(jaggedArray[i][k]+" ");
}
Console.WriteLine();
}

The outcome is:

Another instance exemplifying a jagged array is:

int[][][] jaggedArray2 = new int[2][][]
{
new int[1][]
{
new int[5]{1,2,3,4,5}
},
new int [2][]
{
new int[3]{1,2,3},
new int[4]{1,2,3,4}
}
};

As observed above, a jagged array is defined, comprising two arrays. Accessing elements within these arrays is accomplished through the implementation of nested loops.

for (int i = 0; i < jaggedArray2.Length; i++)// first 
{
for (int k = 0; k < jaggedArray2[i].Length; k++)// jagged arrays
{
for (int j = 0; j < jaggedArray2[i][k].Length; j++)//elements of the jagged arrays
{
Console.Write(jaggedArray2[i][k][j] + " ");
}
Console.WriteLine();
}
}

The outcome is:

Multidimensional Array

At times, there is a requirement for arrays with more dimensions, and we employ [,] to create them.

The notation [,] signifies [Row, Column].

Allow me to illustrate with an example: we can create a multidimensional array, as showcased below.

int[,] multiArray = new int[1, 3]
{
{1,2,3}, // one row and three columns
};

int[,] multiArray2 = new int[2, 5]
{
{1,2,3,4,5 },
{6,7,8,9,10 }
};

To ascertain the dimensionality, the Rank function can be utilized.

var rankOfTheArray = multiArray.Rank; // to get number of dimension
Console.WriteLine(rankOfTheArray);

To determine the quantity of elements within the array, the Length function can be employed.

int lengthOfTheArray = multiArray2.Length; // row * column
Console.WriteLine(lengthOfTheArray);

To access specific elements within the multidimensional array, you can specify the respective row and column indices.

int value = multiArray[0, 0]; // you can access an element of the multidimensional array by specifying a row and column
Console.WriteLine(value);

To access all elements within the array, utilizing a nested for loop is an effective approach.

for (int i = 0; i < multiArray2.GetLength(0); i++) 
{
for (int k = 0; k < multiArray2.GetLength(1); k++)
{
Console.Write(multiArray2[i, k]);
}

Console.WriteLine();
}

Let’s instantiate a three-dimensional array.

int[,,] threeDimension = new int[3, 2, 5] // will contain 3 rows {2 rows and 5 columns}
{
{
{1,2,3,4,5},{6,7,8,9,10},
},
{
{11,12,13,14,15},{16,17,18,19,20},
},
{
{21,22,23,24,25},{26,27,28,29,10},
},
};

Accessing the layers can be accomplished using the GetLength function.

for (int i = 0; i < threeDimension.GetLength(0); i++)
{
for (int k = 0; k < threeDimension.GetLength(1); k++)
{
for (int j = 0; j < threeDimension.GetLength(2); j++)
{
Console.Write(threeDimension[i, k, j]+" ");
}
Console.WriteLine();

}
Console.WriteLine();
}

Let’s delve into the LeetCode question.

Given a 2D integer array nums where nums[i] is a non-empty array of distinct positive integers, return the list of integers that are present in each array of nums sorted in ascending order.

In this particular question, a provided jagged array prompts the task of identifying elements present across all arrays.

To address this, the initial step involves creating a generic list, given the uncertainty about the quantity of elements. (Additionally, the return type is specified as IList.)

List<int> list=new List<int>();

Subsequently, all elements must be added to a Dictionary.

Dictionary<int,int> dic=new Dictionary<int,int>();
for(int i=0;i<nums.GetLength(0);i++)
{
for(int k=0;k<nums[i].Length;k++)
{
if(dic.ContainsKey(nums[i][k]))
{
dic[nums[i][k]] = dic[nums[i][k]]+1;
}
else
{
dic.Add(nums[i][k],1);
}
}
}

The subsequent step entails verifying the count of elements. If the count matches the length of the array, we can identify the sought-after element.

foreach(KeyValuePair<int,int> item in dic)
{
if(item.Value == nums.GetLength(0))
{
list.Add(item.Key);
}
}

Following that, it is imperative to sort the list in ascending order.

list.Sort();

Conclude the process by returning the determined value.

return list;

Here’s the full code, covering everything with a thorough and well-thought-out solution.

List<int> list=new List<int>();

Dictionary<int,int> dic=new Dictionary<int,int>();
for(int i=0;i<nums.GetLength(0);i++)
{
for(int k=0;k<nums[i].Length;k++)
{
if(dic.ContainsKey(nums[i][k]))
{
dic[nums[i][k]] = dic[nums[i][k]]+1;
}
else
{
dic.Add(nums[i][k],1);
}
}
}

foreach(KeyValuePair<int,int> item in dic)
{
if(item.Value == nums.GetLength(0))
{
list.Add(item.Key);
}
}

list.Sort();


return list;

Navigating the nuances of arrays can be a bit tricky in practice, which is why I’ve taken the effort to explain the distinctions and how to access the elements. The code is accessible here

--

--

Firat Tonak
Firat Tonak

Written by Firat Tonak

Seasoned Senior Full-Stack .NET Developer sharing hands-on experience and cutting-edge technologies in comprehensive Full-Stack development. #TechInsights

No responses yet