Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions algorithms/equal_array/cpp/equal_array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// C++ program to find given two array
// are equal or not
#include <bits/stdc++.h>
using namespace std;

// Returns true if arr1[0..n-1] and arr2[0..m-1]
// contain same elements.
bool areEqual(int arr1[], int arr2[], int n, int m)
{
// If lengths of array are not equal means
// array are not equal
if (n != m)
return false;

// Sort both arrays
sort(arr1, arr1 + n);
sort(arr2, arr2 + m);

// Linearly compare elements
for (int i = 0; i < n; i++)
if (arr1[i] != arr2[i])
return false;

// If all elements were same.
return true;
}

// Driver Code
int main()
{
int arr1[] = { 3, 5, 2, 5, 2 };
int arr2[] = { 2, 3, 5, 5, 2 };
int n = sizeof(arr1) / sizeof(int);
int m = sizeof(arr2) / sizeof(int);

if (areEqual(arr1, arr2, n, m))
cout << "Yes";
else
cout << "No";
return 0;
}

44 changes: 44 additions & 0 deletions algorithms/equal_array/java/equal_array.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Java program to find given two array
// are equal or not
import java.io.*;
import java.util.*;

class GFG {
// Returns true if arr1[0..n-1] and arr2[0..m-1]
// contain same elements.
public static boolean areEqual(int arr1[], int arr2[])
{
int n = arr1.length;
int m = arr2.length;

// If lengths of array are not equal means
// array are not equal
if (n != m)
return false;

// Sort both arrays
Arrays.sort(arr1);
Arrays.sort(arr2);

// Linearly compare elements
for (int i = 0; i < n; i++)
if (arr1[i] != arr2[i])
return false;

// If all elements were same.
return true;
}

// Driver code
public static void main(String[] args)
{
int arr1[] = { 3, 5, 2, 5, 2 };
int arr2[] = { 2, 3, 5, 5, 2 };

if (areEqual(arr1, arr2))
System.out.println("Yes");
else
System.out.println("No");
}
}

41 changes: 41 additions & 0 deletions algorithms/equal_array/python/equal_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Python3 program to find given
# two array are equal or not

# Returns true if arr1[0..n-1] and
# arr2[0..m-1] contain same elements.


def areEqual(arr1, arr2, n, m):

# If lengths of array are not
# equal means array are not equal
if (n != m):
return False

# Sort both arrays
arr1.sort()
arr2.sort()

# Linearly compare elements
for i in range(0, n - 1):
if (arr1[i] != arr2[i]):
return False

# If all elements were same.
return True


# Driver Code
arr1 = [3, 5, 2, 5, 2]
arr2 = [2, 3, 5, 5, 2]
n = len(arr1)
m = len(arr2)

if (areEqual(arr1, arr2, n, m)):
print("Yes")
else:
print("No")

# This code is contributed
# by Shivi_Aggarwal.