Advertisement
CR7CR7

compareArr

May 30th, 2022
754
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.84 KB | None | 0 0
  1.  public static boolean areEqual(int arr1[], int arr2[])
  2.     {
  3.         int n = arr1.length;
  4.         int m = arr2.length;
  5.  
  6.         // If lengths of array are not equal means
  7.         // array are not equal
  8.         if (n != m)
  9.             return false;
  10.  
  11.         // Sort both arrays
  12.         Arrays.sort(arr1);
  13.         Arrays.sort(arr2);
  14.  
  15.         // Linearly compare elements
  16.         for (int i = 0; i < n; i++)
  17.             if (arr1[i] != arr2[i])
  18.                 return false;
  19.  
  20.         // If all elements were same.
  21.         return true;
  22.     }
  23.  
  24.     // Driver code
  25.     public static void main(String[] args)
  26.     {
  27.         int arr1[] = { 3, 5, 2, 5, 2 };
  28.         int arr2[] = { 2, 3, 5, 5, 2 };
  29.  
  30.         if (areEqual(arr1, arr2))
  31.             System.out.println("Yes");
  32.         else
  33.             System.out.println("No");
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement