How to check if two arrays have the same set of digits in C#

2 Answers

0 votes
using System;

class Program
{
    static bool have_same_set_of_digits(int[] arr1, int[] arr2) {
        int len1 = arr1.Length;
        int len2 = arr2.Length;
         
        if (len1 != len2)
            return false;
       
        for (int i = 0; i < len1; i++) {
            bool found = false;
            for (int j = 0; j < len1; j++) {
                if (arr1[i] == arr2[j]) {
                    found = true;
                    break;
                }
            }
            if (!found)
                return false;
        }
     
        return true;
    }
    static void Main() {
        int[] arr1 = { 1, 3, 8, 5, 9, 2 };
        int[] arr2 = { 2, 9, 1, 8, 2, 5 };
          
        if (have_same_set_of_digits(arr1, arr2))
              Console.WriteLine("Yes");
        else
             Console.WriteLine("No");
    }
}



/*
run:

No

*/

 



answered Dec 4, 2020 by avibootz
0 votes
using System;
using System.Linq;

class Program
{
    static void Main() {
        int[] arr1 = { 1, 3, 8, 5, 9, 2 };
        int[] arr2 = { 2, 9, 1, 8, 2, 5 };

        bool equals = arr1.OrderBy(a => a).SequenceEqual(arr2.OrderBy(a => a));

        if (equals)
              Console.WriteLine("Yes");
        else
             Console.WriteLine("No");
    }
}



/*
run:

No

*/

 



answered Dec 4, 2020 by avibootz
...