How to compare two arrays in C# if they are Equal or Not.

How to compare two arrays in C# if they are equal or not using 2 methods with code example.

  • By Comparing each corresponding element of both the arrays.
  • Using SequenceEqual() method.

Two arrays will be called equal if both the arrays have same corresponding elements and same number of elements.

Comparing each element of both the arrays for their equality in C#

This is the very simple method in which we will follow below steps to compare array elements and check if both arrays are equal or not.

Steps:

  1. Create 2 arrays with elements.
  2. Check the length of both arrays and compare it.
  3. If they are not equal, then arrays are not equal and no need to process further.
  4. if both the arrays length is equal, then retrieve each corresponding element from both array by traversing within a loop and compare them till last element. If all are equal, then array will be equal or if any of the elements are not equal then arrays will not be equal.

C# Code:

/*-----------------------------------------------------
 * C Sharp program to check if two arrays are equal or not
 */

class Sample
    {
        static void Main(string[] args)
        {
            // Set boolean as true initially
		Boolean isArrayEqual = true;//

		// Declare two Arrays
		int[] arr1 = { 1, 2, 3, 4, 4, 5 };
		int[] arr2 = { 1, 2, 3, 4, 4, 5 };

		// First, Check if length of both array are equal
		// If not equal...no need to traverse array 
		//and make Comparison as they cannot be equal
		if (arr1.Length == arr2.Length) {

			// Traverse both array and compare
			//each element
			for (int i = 0; i < arr2.Length; i++) {

				// set true if each corresponding
				//elements of arrays are equal
				if (arr2[i] != arr1[i]) {

					isArrayEqual = false;
				}
			}
		} else {

			isArrayEqual = false;
		}

		// print the result if both arrays are equal or not
		if (isArrayEqual) {
            Console.WriteLine("Both arrays are equal");
			
		} else {
            Console.WriteLine("Both arrays are not equals");   
			
		}
        }
    }
Input:

int[] arr1 = { 1, 2, 3, 4, 4, 5 };
int[] arr2 = { 1, 2, 3, 4, 4, 5 };

Output:
Both arrays are equal

Input:
int[] arr1 = { 1, 2, 3, 4, 4, 5 };
int[] arr2 = { 1, 2, 3, 4, 4, 5 ,6};

Output:
Both arrays are not equal

How to compare two arrays in c# using SequenceEqual method

We can also compare two arrays for their equality using ASequenceEqual method. The method takes two arguments arr1 and arr2. By comparing both the array it will return a Boolean value true or false if they are equal or not respectively.

C# Code:

/*-----------------------------------------------------
 * C# program to compare two arrays for
 * equality using Arrays.equals method.
 */


class Sample{

	static void Main(string[] args) {

		// Set boolean as true initially
		Boolean isArrayEqual = true;////

		// Declare two Arrays
		int[] arr1 = { 1, 2, 3, 4, 4, 5 };
		int[] arr2 = { 1, 2, 3, 4, 4, 5 };

		//
		isArrayEqual = arr1.SequenceEqual(arr2);

		// Print the result if both arrays are equal or not
		if (isArrayEqual) {
			  Console.WriteLine("Both arrays are equal");
		} else {
			  Console.WriteLine("Both arrays are not equals");   
		}

	}

}

Related Posts