Array Concept in C#:-
It is a collection of elements of Similar and Di similar Types of Elements, We can store multiple-element using a Single Variable in Array. Array Base Index will be started from 0 to size-1.
Type of Array:-
1) One Dimension Array:- using this we can store elements row-wise, array index will be started from 0.
Datatype [] arrayname = {12,23,111,45,67,89};
Datatype [] arrayname = new Datatype[size];
class ArrExample
{
static void Main()
{
int [] arr = new int[5];
for (int i = 0; i < arr.Length; i++)
{
Console.WriteLine("enter element for " + i + " index");
arr[i] = int.Parse(Console.ReadLine());
}
Console.WriteLine("Array Element is ");
for (int i = 0; i < arr.Length; i++)
{
Console.WriteLine(arr[i] +" ");
}
Console.ReadKey();
}
}
Type of Array:-
1) One Dimension Array:- using this we can store elements row-wise, array index will be started from 0.
Datatype [] arrayname = {12,23,111,45,67,89};
Datatype [] arrayname = new Datatype[size];
class ArrExample
{
static void Main()
{
int [] arr = new int[5];
for (int i = 0; i < arr.Length; i++)
{
Console.WriteLine("enter element for " + i + " index");
arr[i] = int.Parse(Console.ReadLine());
}
Console.WriteLine("Array Element is ");
for (int i = 0; i < arr.Length; i++)
{
Console.WriteLine(arr[i] +" ");
}
Console.ReadKey();
}
}
Examples of Array are:-
using System;
class ArrExample
{
static void Main()
{
int size;
Console.WriteLine("Enter size of elements");
size = int.Parse(Console.ReadLine());
int [] arr = new int[size];
for(int i=0;i<size;i++)
{
Console.WriteLine("Enter element for "+i+ " index");
arr[i]= int.Parse(Console.ReadLine());
}
Console.WriteLine("Array elements are");
for(int i=0;i<size;i++)
{
Console.WriteLine(arr[i]);
}
Console.WriteLine("Reverse of array elements are");
for(int i=size-1;i>=0;i--)
{
Console.WriteLine(arr[i]);
}
Console.WriteLine("Max elements");
int max=arr[0];
int smax= arr[0];
for(int i=1;i<size;i++)
{
if(max<arr[i])
{
smax=max;
max=arr[i];
}
else if(smax<arr[i])
smax=arr[i];
}
Console.WriteLine(max + " "+smax);
}
}
2) Two Dimension Array or Multi Dimension Array in C#:-
It will Store elements using rows and columns, we can display data in matrix patterns using Multi dimension Arrays.
Datatype [,] arrayname = new Datayype[row,column];
int [][] arr = new int[2,2];
Datatype [,] arrayname = new Datayype[row,column];
int [][] arr = new int[2,2];
Example of Multidimension Array:-
class MultiArr
{
static void Main()
{
int [,] arr = new int[2,2];
for (int i = 0; i < 2; i++)
{
for (int j = 0; j <2; j++)
{
Console.WriteLine("enter element for " + i +""+ j+ " index");
arr[i,j] = int.Parse(Console.ReadLine());
}
}
Console.WriteLine("Array Element is ");
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
Console.Write(arr[i, j] + " ");
}
Console.WriteLine();
}
Console.ReadKey();
}
}
Program to find max element for each row under 2-D Array:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BasicExampleNew
{
class MultiDimensionArray
{
static void Main()
{
int r=2, c=3;
int[,] arr = new int[r, c];
for (int i = 0; i <r; i++)
{
for (int j = 0; j < c; j++)
{
Console.WriteLine("Enter element for {0}{1} index",i,j);
arr[i, j] = int.Parse(Console.ReadLine());
}
}
Console.WriteLine("Array Elements Are");
for (int i = 0; i < r; i++)
{
int max = arr[i,0];
for (int j = 0; j < c; j++)
{
if (max < arr[i, j])
{
max = arr[i, j];
}
Console.Write(arr[i,j] + " ");
}
Console.WriteLine(" Max: " + max);
}
Console.ReadKey();
}
}
}
# Program to sort the element for each row?
class MultiDimensionArray
{
static void Main()
{
int r=2, c=3;
int[,] arr = new int[r, c];
for (int i = 0; i <r; i++)
{
for (int j = 0; j < c; j++)
{
Console.WriteLine("Enter element for {0}{1} index",i,j);
arr[i, j] = int.Parse(Console.ReadLine());
}
}
Console.WriteLine("Array Elements Are");
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
for (int k = j + 1; k < c; k++)
{
if (arr[i,j] > arr[i,k])
{
int temp = arr[i, j];
arr[i, j] = arr[i, k];
arr[i, k] = temp;
}
}
Console.Write(arr[i,j] + " ");
}
Console.WriteLine();
}
Console.ReadKey();
}
}
3) Jagged Array::-
Jagged is the collection of heterogeneous row elements means we can declare a different number of elements for each row.
Datatype [][] arrayname = new Datatype[rowsize][];
int [][] arr = new int[3][];
arr[0] = new int[5];
arr[1] = new int[2];
arr[2] = new int[1];
Program of Jagged Array:-
Datatype [][] arrayname = new Datatype[rowsize][];
int [][] arr = new int[3][];
arr[0] = new int[5];
arr[1] = new int[2];
arr[2] = new int[1];
Program of Jagged Array:-
class JaggedArr
{
public static void Main()
{
int[][] arr = new int[2][];
arr[0] = new int[5]{1,5,3,2,7};
arr[1] = new int[2]{3,6};
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < arr[i].Length; j++)
{
Console.Write(arr[i][j] + " ");
}
Console.WriteLine();
}
Console.ReadKey();
}
}
{
static void Main()
{
int r,c;
Console.WriteLine("Enter number of rows");
r = int.Parse(Console.ReadLine());
int[][] arr = new int[r][];
for (int i = 0; i < r; i++)
{
Console.WriteLine("Enter number of column elements");
c = int.Parse(Console.ReadLine());
arr[i] = new int[c];
for (int j = 0; j < c; j++)
{
arr[i][j] = int.Parse(Console.ReadLine());
}
}
Console.WriteLine("Jagged Array Elements Is");
for (int i = 0; i < r; i++)
{
for (int j = 0; j < arr[i].Length; j++)
{
Console.Write(arr[i][j] +" ");
}
Console.WriteLine();
}
Console.ReadKey();
}
}
4) Object Array
class ObjectArr
{
static void Main()
{
object[] arr = {"C","CPP",1200,12.34F,true,'a' };
for (int i = 0; i < arr.Length; i++)
{
Console.WriteLine(arr[i]);
}
Console.ReadKey();
}
}
Program of Split one Array to two different Sub-Array?
program to sort the elements of the array in desc order */
/* Program to find a unique element in the array */
WAP to find the max element in each row and complete matrix?
Q)WAP to Check Palindrome using Char Array?
Q) WAP to print the reverse of the Char Array?
Q) WAP to count total vowel and consonant in Char Array?
Solution:-
class VC
{
static void Main()
{
// char[] arr = { 'c', 'o', 'n', 'c', 'e', 'p', 't' };
Console.WriteLine("Enter name");
string arr = Console.ReadLine();
int v = 0, c = 0;
for (int i = 0; i < arr.Length; i++)
{
if (arr[i] == 'a' || arr[i] == 'e' || arr[i] == 'i' || arr[i] == 'o' || arr[i] == 'u')
{
v++;
}
else
{
c++;
}
}
Console.WriteLine("Total Vowel is {0} and Consonent is {1}", v, c);
Console.ReadKey();
}
}
Q) WAP to replace char array each char to the next consecutive char?
class VC
{
static void Main()
{
char[] arr = { 'c', 'o', 'n', 'c', 'e', 'p', 'z' };
int v;
for (int i = 0; i < arr.Length; i++)
{
v = arr[i] + 1;
if (arr[i] == 'z')
{
arr[i] = 'a';
}
else
{
arr[i] = (char)v;
}
Console.WriteLine(arr[i]);
}
Console.ReadKey();
}
}
Q) WAP to sort the elements of the array?
class SortArray
{
static void Main()
{
int [] arr = { 10,11,23,34,67,89,2,67};
int c;
for (int i = 0; i < arr.Length; i++)
{
for (int j = i + 1; j < arr.Length; j++)
{
if (arr[i] > arr[j])
{
c = arr[i];
arr[i] = arr[j];
arr[j] = c;
}
}
}
Console.WriteLine("Sorted array is ");
for (int i = 0; i < arr.Length; i++)
{
Console.WriteLine(arr[i]);
}
Console.ReadKey();
}
}
Q) WAP to merge two arrays in one array?
Solution:-
class VC
{
static void Main()
{
int [] a = { 1,2};
int [] b = { 3, 4 };
int [] c = new int[a.Length+b.Length];
int index = 0;
for (int i = 0; i < a.Length; i++)
{
c[index] = a[i];
index++;
}
for (int i = 0; i < b.Length; i++)
{
c[index] = b[i];
index++;
}
for (int i = 0; i < c.Length; i++)
{
Console.WriteLine(c[i]);
}
Console.ReadKey();
}
}
Q) WAP to split the one-dimensional array into two different subarrays?
Q) WAP to calculate Sum of even elements and odd elements of the array?
Q) WAP to convert Char Array to upper case to lower case and lower case to Upper case
Q) WAP to perform the addition of two matrices?
class MatrixAddition
{
static void Main()
{
int[,] x = { { 1, 2,3 }, { 3, 4,5 } };
int[,] y = { { 2, 3,5 }, { 4, 5,9} };
int[,] z = new int[2,3];
int i, j;
Console.WriteLine("First Matrix is ");
for (i = 0; i < 2; i++)
{
for (j = 0; j < 3; j++)
{
Console.Write(x[i, j] + " ");
}
Console.WriteLine();
}
Console.WriteLine("Second Matrix is ");
for (i = 0; i < 2; i++)
{
for (j = 0; j < 3; j++)
{
Console.Write(y[i, j] + " ");
}
Console.WriteLine();
}
Console.WriteLine("Addition of Matrix is ");
for (i = 0; i < 2; i++)
{
for (j = 0; j < 3; j++)
{
// Console.Write((x[i,j]+y[i, j]) + " ");
z[i, j] = x[i, j] + y[i, j];
Console.Write(z[i, j] + " ");
}
Console.WriteLine();
}
Console.ReadKey();
}
}
Q) WAP to multiply two matrices?
Solution:-
class MatrixAddition
{
static void Main()
{
int[,] x = { { 1, 2}, { 3, 4 } };
int[,] y = { { 2, 3 }, { 4, 5} };
int[,] z = new int[2,2];
int i, j,k;
Console.WriteLine("First Matrix is ");
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
{
Console.Write(x[i, j] + " ");
}
Console.WriteLine();
}
Console.WriteLine("Second Matrix is ");
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
{
Console.Write(y[i, j] + " ");
}
Console.WriteLine();
}
Console.WriteLine("Multiplication of Matrix is ");
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
{
int sum = 0;
for (k = 0; k < 2; k++)
{
sum = sum + x[i, k] * y[k, j];
}
z[i,j]=sum;
Console.Write(z[i, j] + " ");
}
Console.WriteLine();
}
Console.ReadKey();
}
}
Classroom Example:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OOPSConsoleApp
{
class Program
{
int [] arr;
int[,] arr1;
int[][] arr2;
int r, c;
void AcceptArr(int [] arr) //One Dimension Array
{
this.arr = arr;
}
void AcceptArr1(int[,] arr1,int r,int c) // //Multi Dimension Array
{
this.r = r;
this.c = c;
this.arr1 = arr1;
}
void AcceptArr2(int[][] arr2) //Jagged Array
{
this.arr2 = arr2;
}
void DisplayArr()
{
foreach (int result in arr)
{
Console.WriteLine(result);
}
}
void DisplayArr1()
{
for (int i = 0; i < r;i++ )
{
for(int j=0;j<c;j++)
{
Console.Write(arr1[i,j] + " ");
}
Console.WriteLine();
}
}
void DisplayArr2()
{
foreach (int [] result in arr2)
{
foreach (int res in result)
{
Console.Write(res + " ");
}
Console.WriteLine();
}
}
int [] SortArr()
{
for (int i = 0; i < arr.Length; i++)
{
for (int j = i + 1; j < arr.Length; j++)
{
if (arr[i] < arr[j])
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
return arr;
}
static void Main(string[] args)
{
/* Program p = null;
p=new Program();
int[] arr = { 12, 23, 34, 78, 89, 11 };
p.AcceptArr(arr);
p.DisplayArr();
Console.WriteLine("After Sorting");
int [] arr1= p.SortArr();
foreach(int arr2 in arr1)
{
Console.WriteLine(arr2);
}*/
Console.WriteLine("MultiDimension Array");
int[,] arr = { { 2, 3 }, { 3, 4 } };
Program parr = new Program();
parr.AcceptArr1(arr,2,2);
parr.DisplayArr1();
Console.WriteLine("Jagged Array");
int[][] arr1 = new int[3][];
arr1[0] = new int[3] { 1, 4, 8 };
arr1[1] = new int[5] { 2, 3, 4, 9, 11 };
arr1[2] = new int[2] { 2, 3};
parr.AcceptArr2(arr1);
parr.DisplayArr2();
Console.ReadKey();
}
}
}
Classroom Example2:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OOPSConsoleApp
{
class ObjectArrExample
{
object[] arr;
void AcceptObject(object [] arr)
{
this.arr = arr;
}
void DisplayObject()
{
foreach(object o in arr)
{
Console.WriteLine(o);
}
}
public static void Main()
{
ObjectArrExample obj = new ObjectArrExample();
object [] a = {1001,"XYZ","CS",45000.345};
obj.AcceptObject(a);
obj.DisplayObject();
Console.ReadKey();
}
}
}
Q) WAP to find the second max element in each row of the matrix?
Q) WAP to count the total prime element in the matrix?
Q) WAP to find an average of Matrix rows?
Q) WAP to sort matrix rows?
MOST Important Array Interview Questions for Interview?
Check if a key is present in every segment of size k in an array
Find the minimum and maximum element in an array
Write a program to reverse the array
Write a program to sort the given array
Find the Kth largest and Kth smallest number in an array
Find the occurrence of an integer in the array
Sort the array of 0s, 1s, and 2s
Range and Coefficient of array
Move all the negative elements to one side of the array
Find the Union and Intersection of the two sorted arrays
Level 2
Write a program to cyclically rotate an array by one
Find the missing integer
Count Pairs with given sum
Find duplicates in an array
Sort an Array using the Quicksort algorithm
Find common elements in three sorted arrays
Find the first repeating element in an array of integers
Find the first non-repeating element in a given array of integers
Find the largest three elements in an array Time
Rearrange the array in alternating positive and negative items
Find if there is any subarray with sum equal to zero
Find Largest sum contiguous Subarray
Find the factorial of a large number
Find Maximum Product Subarray
Find longest consecutive subsequence
Find the minimum element in a rotated and sorted array
Find all elements that appear more than N/K times
GCD of given index ranges in an array
Minimize the maximum difference between the heights
Minimum number of jumps to reach the end
Find the two repetitive elements in a given array
Find a triplet that sums to a given value
Construct a N*M matrix from the user input
Find the row with the maximum number of 1’s
Print the matrix in a Spiral manner
Find whether an array is a subset of another array
Implement two Stacks in an array
Majority Element
Wave Array
Trapping Rainwater
Level 3
Maximum Index
Max sum path in two arrays
Find Missing And Repeating
Stock buy and sell Problem
Pair with given sum in a sorted array
Chocolate Distribution Problem
Longest Consecutive Subsequence
Print all possible combinations of r elements in a given array
WAP to print reverse of Char Array?
ردحذفusing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ReverseofArray
{
class Program
{
static void Main(string[] args)
{
char[] arr = { 'A', 'a', 'd', 'a', 'r','s','h' };
Console.WriteLine("Reverse of char ");
for (int i= arr.Length-1 ; i>=0 ;i--)
{
Console.WriteLine(arr[i]);
}
Console.ReadKey();
}
}
}
program to sort the element of array in desc order
ردحذفusing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace sorting
{
class Program
{
static void Main(string[] args)
{
int[] Arr = new int[5];
int i,j,temp;
for (i = 0; i < Arr.Length; i++)
{
Console.WriteLine("enter element of array" + i+"Index");
Arr[i] = int.Parse(Console.ReadLine());
}
for(i=0;i<Arr.Length;i++)
{
for(j=i+1;j<Arr.Length;j++)
{
if (Arr[i] < Arr[j])
{
temp = Arr[i];
Arr[i] = Arr[j];
Arr[j] = temp;
}
}
}
for(i=0;i<=Arr.Length;i++)
{
Console.WriteLine("sort of array is " + Arr[i]);
}
Console.ReadKey();
}
}
}
#SHIVA
ردحذف{
char [] ar = {'A','B','C','D','E','X','Y','Z'};
for(int i=ar.Length-1; i>=0; i--)
{
Console.Write(ar[i]+" ");
}
Console.WriteLine();
}
#Shiva
ردحذفLowercase to uppercase & uppercase to lowercase
{
char[] aa = {'i'};
int ab = 0;
for(int i=0; i='A' && aa[i]<='Z')
{
ab= ab+(char)aa[i]+32;
}
else
ab = ab+(char)aa[i]-32;
}
Console.WriteLine("Result is: {0}",(char)ab);
}
# Shiva
ردحذفSum of EVEN & ODD Elements
{
int[] A = {1,2,3,4,5,6,7};
int SE =0,SO=0;
for(int i=0; i<A.Length; i++)
{
if(A[i]%2==0)
{
SE = SE + A[i];
}
else
{
SO = SO + A[i];
}
}
Console.WriteLine("Sum of Odd : {0} \nSum of Even {1}",SO,SE);
}
using System;
ردحذفclass Practice
{
static bool findSize(int[] arr, int x,
int k, int n)
{
int i;
for (i = 0; i < n; i = i + k)
{
int j;
for (j = 0; j < k; j++)
if (arr[i + j] == x)
break;
if (j == k)
return false;
}
if (i == n)
return true;
int l;
for (l = i - k; l < n; l++)
if (arr[l] == x)
break;
if (l == n)
return false;
return true;
}
public static void Main()
{
int[] arr = new int[] {3, 5, 2, 4, 9, 3,
1, 7, 3, 11, 12, 3};
int x = 3, k = 3;
int n = arr.Length;
if (findSize(arr, x, k, n))
Console.Write("Yes");
else
Console.Write("No");
}
}
using System;
ردحذفpublic class Demo {
public static void Main() {
int[] arr = new int[5] {10,15,14,20,25};
int i, max, min, n;
n = 5;
max = arr[0];
min = arr[0];
for(i=1; imax) {
max = arr[i];
}
if(arr[i]<min) {
min = arr[i];
}
}
Console.Write("Maximum element = {0}\n", max);
Console.Write("Minimum element = {0}\n\n", min);
}
}
Unique
ردحذف{
int[] arr = { 2, 4, 9, 7, 8, 9, 1, 2, 2, 7 };
for (int i = 0; i < arr.Length; i++)
{
bool f = true;
for (int j = 0;j < arr.Length; j++)
{
if (arr[i] == arr[j] && i != j)
{
f=false;
break;
}
}
if (f)
{
Console.WriteLine(arr[i]);
}
}
}