Check if two arrays are permutations of each other
Here given code implementation process.
/*
Java Program
Check if two arrays are permutations of each other
*/
import java.util.HashMap;
public class Permutations
{
// Function which is display array elements
public void display(int[] arr, int n)
{
for (int i = 0; i < n; ++i)
{
System.out.print(" " + arr[i]);
}
System.out.print("\n");
}
// Check that given two arrays are permutations of each other or not
public void isPermutations(int[] arr1, int[] arr2)
{
// Get the length
int n = arr1.length;
int m = arr2.length;
System.out.print("\n Array A : ");
// Display given array arr1
display(arr1, n);
System.out.print(" Array B : ");
// Display given array arr2
display(arr2, m);
boolean result = false;
if (n == m)
{
result = true;
// Loop controlling variable
int i = 0;
// Use to collect frequency of array elements
HashMap < Integer, Integer > store = new HashMap < Integer, Integer > ();
// Get frequency of first array
for (i = 0; i < n; ++i)
{
if (store.containsKey(arr1[i]))
{
store.put(arr1[i], store.get(arr1[i]) + 1);
}
else
{
store.put(arr1[i], 1);
}
}
// Check permutation elements in second array
for (i = 0; i < m && result == true; ++i)
{
if (store.containsKey(arr2[i]) && store.get(arr2[i]) > 0)
{
// When element frequency is more than one
// Reduce its frequency by one
store.replace(arr2[i], store.get(arr2[i]) - 1);
}
else
{
// In case element not exist in store
result = false;
}
}
}
if (result)
{
System.out.print(" Output : Yes \n");
}
else
{
System.out.print(" Output : No \n");
}
}
public static void main(String[] arg)
{
Permutations task = new Permutations();
// Define arrays of integer elements
int[] arr1 = {
8 , 5 , 7 , 9 , 4 , 7
};
int[] arr2 = {
9 , 5 , 7 , 7 , 9 , 8
};
int[] arr3 = {
9 , 7 , 5 , 7 , 8 , 4
};
// Test cases
task.isPermutations(arr1, arr2);
task.isPermutations(arr1, arr3);
}
}
Output
Array A : 8 5 7 9 4 7
Array B : 9 5 7 7 9 8
Output : No
Array A : 8 5 7 9 4 7
Array B : 9 7 5 7 8 4
Output : Yes
// Include header file
#include <iostream>
#include <unordered_map>
using namespace std;
class Permutations
{
public:
// Function which is display array elements
void display(int arr[], int n)
{
for (int i = 0; i < n; ++i)
{
cout << " " << arr[i];
}
cout << "\n";
}
// Check that given two arrays are permutations of each other or not
void isPermutations(int arr1[], int arr2[], int n, int m)
{
cout << "\n Array A : ";
// Display given array arr1
this->display(arr1, n);
cout << " Array B : ";
// Display given array arr2
this->display(arr2, m);
bool result = false;
if (n == m)
{
result = true;
// Loop controlling variable
int i = 0;
// Use to collect frequency of array elements
unordered_map < int, int > store ;
// Get frequency of first array
for (i = 0; i < n; ++i)
{
if (store.find(arr1[i]) != store.end())
{
store[arr1[i]] = store[arr1[i]] + 1;
}
else
{
store[arr1[i]] = 1;
}
}
// Check permutation elements in second array
for (i = 0; i < m && result == true; ++i)
{
if (store.find(arr2[i]) != store.end() && store[arr2[i]] > 0)
{
// When element frequency is more than one
// Reduce its frequency by one
store[arr2[i]] = store[arr2[i]] - 1;
}
else
{
// In case element not exist in store
result = false;
}
}
}
if (result)
{
cout << " Output : Yes \n";
}
else
{
cout << " Output : No \n";
}
}
};
int main()
{
Permutations task = Permutations();
// Define arrays of integer elements
int arr1[] = {
8 , 5 , 7 , 9 , 4 , 7
};
int arr2[] = {
9 , 5 , 7 , 7 , 9 , 8
};
int arr3[] = {
9 , 7 , 5 , 7 , 8 , 4
};
// Test cases
// Get the length
int n = sizeof(arr1) / sizeof(arr1[0]);
int m = sizeof(arr2) / sizeof(arr2[0]);
task.isPermutations(arr1, arr2, n, m);
m = sizeof(arr3) / sizeof(arr3[0]);
task.isPermutations(arr1, arr3, n, m);
return 0;
}
Output
Array A : 8 5 7 9 4 7
Array B : 9 5 7 7 9 8
Output : No
Array A : 8 5 7 9 4 7
Array B : 9 7 5 7 8 4
Output : Yes
// Include namespace system
using System;
using System.Collections.Generic;
/*
C# Program
Check if two arrays are permutations of each other
*/
public class Permutations
{
// Function which is display array elements
public void display(int[] arr, int n)
{
for (int i = 0; i < n; ++i)
{
Console.Write(" " + arr[i]);
}
Console.Write("\n");
}
// Check that given two arrays are permutations of each other or not
public void isPermutations(int[] arr1, int[] arr2)
{
// Get the length
int n = arr1.Length;
int m = arr2.Length;
Console.Write("\n Array A : ");
// Display given array arr1
display(arr1, n);
Console.Write(" Array B : ");
// Display given array arr2
display(arr2, m);
Boolean result = false;
if (n == m)
{
result = true;
// Loop controlling variable
int i = 0;
// Use to collect frequency of array elements
Dictionary < int, int > store = new Dictionary < int, int > ();
// Get frequency of first array
for (i = 0; i < n; ++i)
{
if (store.ContainsKey(arr1[i]))
{
store[arr1[i]] = store[arr1[i]] + 1;
}
else
{
store.Add(arr1[i], 1);
}
}
// Check permutation elements in second array
for (i = 0; i < m && result == true; ++i)
{
if (store.ContainsKey(arr2[i]) && store[arr2[i]] > 0)
{
// When element frequency is more than one
// Reduce its frequency by one
store[arr2[i]] = store[arr2[i]] - 1;
}
else
{
// In case element not exist in store
result = false;
}
}
}
if (result)
{
Console.Write(" Output : Yes \n");
}
else
{
Console.Write(" Output : No \n");
}
}
public static void Main(String[] arg)
{
Permutations task = new Permutations();
// Define arrays of integer elements
int[] arr1 = {
8 , 5 , 7 , 9 , 4 , 7
};
int[] arr2 = {
9 , 5 , 7 , 7 , 9 , 8
};
int[] arr3 = {
9 , 7 , 5 , 7 , 8 , 4
};
// Test cases
task.isPermutations(arr1, arr2);
task.isPermutations(arr1, arr3);
}
}
Output
Array A : 8 5 7 9 4 7
Array B : 9 5 7 7 9 8
Output : No
Array A : 8 5 7 9 4 7
Array B : 9 7 5 7 8 4
Output : Yes
<?php
/*
Php Program
Check if two arrays are permutations of each other
*/
class Permutations
{
// Function which is display array elements
public function display($arr, $n)
{
for ($i = 0; $i < $n; ++$i)
{
echo " ". $arr[$i];
}
echo "\n";
}
// Check that given two arrays are permutations of each other or not
public function isPermutations($arr1, $arr2)
{
// Get the length
$n = count($arr1);
$m = count($arr2);
echo "\n Array A : ";
// Display given array arr1
$this->display($arr1, $n);
echo " Array B : ";
// Display given array arr2
$this->display($arr2, $m);
$result = false;
if ($n == $m)
{
$result = true;
// Loop controlling variable
$i = 0;
// Use to collect frequency of array elements
$store = array();
// Get frequency of first array
for ($i = 0; $i < $n; ++$i)
{
if (array_key_exists($arr1[$i], $store))
{
$store[$arr1[$i]] = $store[$arr1[$i]] + 1;
}
else
{
$store[$arr1[$i]] = 1;
}
}
// Check permutation elements in second array
for ($i = 0; $i < $m && $result == true; ++$i)
{
if (array_key_exists($arr2[$i], $store) && $store[$arr2[$i]] > 0)
{
$store[$arr2[$i]] = $store[$arr2[$i]] - 1;
}
else
{
// In case element not exist in store
$result = false;
}
}
}
if ($result)
{
echo " Output : Yes \n";
}
else
{
echo " Output : No \n";
}
}
}
function main()
{
$task = new Permutations();
// Define arrays of integer elements
$arr1 = array(8, 5, 7, 9, 4, 7);
$arr2 = array(9, 5, 7, 7, 9, 8);
$arr3 = array(9, 7, 5, 7, 8, 4);
// Test cases
$task->isPermutations($arr1, $arr2);
$task->isPermutations($arr1, $arr3);
}
main();
Output
Array A : 8 5 7 9 4 7
Array B : 9 5 7 7 9 8
Output : No
Array A : 8 5 7 9 4 7
Array B : 9 7 5 7 8 4
Output : Yes
/*
Node Js Program
Check if two arrays are permutations of each other
*/
class Permutations
{
// Function which is display array elements
display(arr, n)
{
for (var i = 0; i < n; ++i)
{
process.stdout.write(" " + arr[i]);
}
process.stdout.write("\n");
}
// Check that given two arrays are permutations of each other or not
isPermutations(arr1, arr2)
{
// Get the length
var n = arr1.length;
var m = arr2.length;
process.stdout.write("\n Array A : ");
// Display given array arr1
this.display(arr1, n);
process.stdout.write(" Array B : ");
// Display given array arr2
this.display(arr2, m);
var result = false;
if (n == m)
{
result = true;
// Loop controlling variable
var i = 0;
// Use to collect frequency of array elements
var store = new Map();
// Get frequency of first array
for (i = 0; i < n; ++i)
{
if (store.has(arr1[i]))
{
store.set(arr1[i], store.get(arr1[i]) + 1);
}
else
{
store.set(arr1[i], 1);
}
}
// Check permutation elements in second array
for (i = 0; i < m && result == true; ++i)
{
if (store.has(arr2[i]) && store.get(arr2[i]) > 0)
{
// When element frequency is more than one
// Reduce its frequency by one
store.set(arr2[i], store.get(arr2[i]) - 1);
}
else
{
// In case element not exist in store
result = false;
}
}
}
if (result)
{
process.stdout.write(" Output : Yes \n");
}
else
{
process.stdout.write(" Output : No \n");
}
}
}
function main()
{
var task = new Permutations();
// Define arrays of integer elements
var arr1 = [8, 5, 7, 9, 4, 7];
var arr2 = [9, 5, 7, 7, 9, 8];
var arr3 = [9, 7, 5, 7, 8, 4];
// Test cases
task.isPermutations(arr1, arr2);
task.isPermutations(arr1, arr3);
}
main();
Output
Array A : 8 5 7 9 4 7
Array B : 9 5 7 7 9 8
Output : No
Array A : 8 5 7 9 4 7
Array B : 9 7 5 7 8 4
Output : Yes
# Python 3 Program
# Check if two arrays are permutations of each other
class Permutations :
# Function which is display list elements
def display(self, arr, n) :
i = 0
while (i < n) :
print(" ", arr[i], end = "")
i += 1
print(end = "\n")
# Check that given two lists are permutations of each other or not
def isPermutations(self, arr1, arr2) :
# Get the length
n = len(arr1)
m = len(arr2)
print("\n Array A : ", end = "")
# Display given list arr1
self.display(arr1, n)
print(" Array B : ", end = "")
# Display given list arr2
self.display(arr2, m)
result = False
if (n == m) :
result = True
# Loop controlling variable
i = 0
# Use to collect frequency of list elements
store = dict()
# Get frequency of first list
while (i < n) :
if (arr1[i] in store.keys()) :
store[arr1[i]] = store.get(arr1[i]) + 1
else :
store[arr1[i]] = 1
i += 1
i = 0
# Check permutation elements in second list
while (i < m and result == True) :
if (arr2[i] in store.keys() and store.get(arr2[i]) > 0) :
# When element frequency is more than one
# Reduce its frequency by one
store[arr2[i]] = store.get(arr2[i]) - 1
else :
# In case element not exist in store
result = False
i += 1
if (result) :
print(" Output : Yes")
else :
print(" Output : No")
def main() :
task = Permutations()
# Define lists of integer elements
arr1 = [8, 5, 7, 9, 4, 7]
arr2 = [9, 5, 7, 7, 9, 8]
arr3 = [9, 7, 5, 7, 8, 4]
# Test cases
task.isPermutations(arr1, arr2)
task.isPermutations(arr1, arr3)
if __name__ == "__main__": main()
Output
Array A : 8 5 7 9 4 7
Array B : 9 5 7 7 9 8
Output : No
Array A : 8 5 7 9 4 7
Array B : 9 7 5 7 8 4
Output : Yes
# Ruby Program
# Check if two arrays are permutations of each other
class Permutations
# Function which is display array elements
def display(arr, n)
i = 0
while (i < n)
print(" ", arr[i])
i += 1
end
print("\n")
end
# Check that given two arrays are permutations of each other or not
def isPermutations(arr1, arr2)
# Get the length
n = arr1.length
m = arr2.length
print("\n Array A : ")
# Display given array arr1
self.display(arr1, n)
print(" Array B : ")
# Display given array arr2
self.display(arr2, m)
result = false
if (n == m)
result = true
# Loop controlling variable
i = 0
# Use to collect frequency of array elements
store = Hash.new
# Get frequency of first array
while (i < n)
if (store.key?(arr1[i]))
store[arr1[i]] = store[arr1[i]] + 1
else
store[arr1[i]] = 1
end
i += 1
end
i = 0
# Check permutation elements in second array
while (i < m && result == true)
if (store.key?(arr2[i]) && store[arr2[i]] > 0)
store[arr2[i]] = store[arr2[i]] - 1
else
# In case element not exist in store
result = false
end
i += 1
end
end
if (result)
print(" Output : Yes \n")
else
print(" Output : No \n")
end
end
end
def main()
task = Permutations.new()
# Define arrays of integer elements
arr1 = [8, 5, 7, 9, 4, 7]
arr2 = [9, 5, 7, 7, 9, 8]
arr3 = [9, 7, 5, 7, 8, 4]
# Test cases
task.isPermutations(arr1, arr2)
task.isPermutations(arr1, arr3)
end
main()
Output
Array A : 8 5 7 9 4 7
Array B : 9 5 7 7 9 8
Output : No
Array A : 8 5 7 9 4 7
Array B : 9 7 5 7 8 4
Output : Yes
import scala.collection.mutable._;
/*
Scala Program
Check if two arrays are permutations of each other
*/
class Permutations
{
// Function which is display array elements
def display(arr: Array[Int], n: Int): Unit = {
var i: Int = 0;
while (i < n)
{
print(" " + arr(i));
i += 1;
}
print("\n");
}
// Check that given two arrays are permutations of each other or not
def isPermutations(arr1: Array[Int], arr2: Array[Int]): Unit = {
// Get the length
var n: Int = arr1.length;
var m: Int = arr2.length;
print("\n Array A : ");
// Display given array arr1
this.display(arr1, n);
print(" Array B : ");
// Display given array arr2
this.display(arr2, m);
var result: Boolean = false;
if (n == m)
{
result = true;
// Loop controlling variable
var i: Int = 0;
// Use to collect frequency of array elements
var store: Map[Int, Int] = Map();
// Get frequency of first array
while (i < n)
{
if (store.contains(arr1(i)))
{
store.addOne(arr1(i), store.get(arr1(i)).get + 1);
}
else
{
store.addOne(arr1(i), 1);
}
i += 1;
}
i = 0;
// Check permutation elements in second array
while (i < m && result == true)
{
if (store.contains(arr2(i)) && store.get(arr2(i)).get > 0)
{
// When element frequency is more than one
// Reduce its frequency by one
store.addOne(arr2(i), store.get(arr2(i)).get - 1);
}
else
{
// In case element not exist in store
result = false;
}
i += 1;
}
}
if (result)
{
print(" Output : Yes \n");
}
else
{
print(" Output : No \n");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Permutations = new Permutations();
// Define arrays of integer elements
var arr1: Array[Int] = Array(8, 5, 7, 9, 4, 7);
var arr2: Array[Int] = Array(9, 5, 7, 7, 9, 8);
var arr3: Array[Int] = Array(9, 7, 5, 7, 8, 4);
// Test cases
task.isPermutations(arr1, arr2);
task.isPermutations(arr1, arr3);
}
}
Output
Array A : 8 5 7 9 4 7
Array B : 9 5 7 7 9 8
Output : No
Array A : 8 5 7 9 4 7
Array B : 9 7 5 7 8 4
Output : Yes
import Foundation
/*
Swift 4 Program
Check if two arrays are permutations of each other
*/
class Permutations
{
// Function which is display array elements
func display(_ arr: [Int], _ n: Int)
{
var i: Int = 0;
while (i < n)
{
print(" ", arr[i], terminator: "");
i += 1;
}
print(terminator: "\n");
}
// Check that given two arrays are permutations of each other or not
func isPermutations(_ arr1: [Int], _ arr2: [Int])
{
// Get the length
let n: Int = arr1.count;
let m: Int = arr2.count;
print("\n Array A : ", terminator: "");
// Display given array arr1
self.display(arr1, n);
print(" Array B : ", terminator: "");
// Display given array arr2
self.display(arr2, m);
var result: Bool = false;
if (n == m)
{
result = true;
// Loop controlling variable
var i: Int = 0;
// Use to collect frequency of array elements
var store = [Int: Int]();
// Get frequency of first array
while (i < n)
{
if (store.keys.contains(arr1[i]))
{
store[arr1[i]] = store[arr1[i]]! + 1;
}
else
{
store[arr1[i]] = 1;
}
i += 1;
}
i = 0;
// Check permutation elements in second array
while (i < m && result == true)
{
if (store.keys.contains(arr2[i]) && store[arr2[i]]! > 0)
{
// When element frequency is more than one
// Reduce its frequency by one
store[arr2[i]] = store[arr2[i]]! - 1;
}
else
{
// In case element not exist in store
result = false;
}
i += 1;
}
}
if (result)
{
print(" Output : Yes ");
}
else
{
print(" Output : No ");
}
}
}
func main()
{
let task: Permutations = Permutations();
// Define arrays of integer elements
let arr1: [Int] = [8, 5, 7, 9, 4, 7];
let arr2: [Int] = [9, 5, 7, 7, 9, 8];
let arr3: [Int] = [9, 7, 5, 7, 8, 4];
// Test cases
task.isPermutations(arr1, arr2);
task.isPermutations(arr1, arr3);
}
main();
Output
Array A : 8 5 7 9 4 7
Array B : 9 5 7 7 9 8
Output : No
Array A : 8 5 7 9 4 7
Array B : 9 7 5 7 8 4
Output : Yes
/*
Kotlin Program
Check if two arrays are permutations of each other
*/
class Permutations
{
// Function which is display array elements
fun display(arr: Array < Int > , n: Int): Unit
{
var i: Int = 0;
while (i < n)
{
print(" " + arr[i]);
i += 1;
}
print("\n");
}
// Check that given two arrays are permutations of each other or not
fun isPermutations(arr1: Array < Int > , arr2: Array < Int > ): Unit
{
// Get the length
var n: Int = arr1.count();
var m: Int = arr2.count();
print("\n Array A : ");
// Display given array arr1
this.display(arr1, n);
print(" Array B : ");
// Display given array arr2
this.display(arr2, m);
var result: Boolean = false;
if (n == m)
{
result = true;
// Loop controlling variable
var i: Int = 0;
// Use to collect frequency of array elements
var store = mutableMapOf<Int, Int>();
// Get frequency of first array
while (i < n)
{
if (store.containsKey(arr1[i]))
{
store.put(arr1[i], store.getValue(arr1[i]) + 1);
}
else
{
store.put(arr1[i], 1);
}
i += 1;
}
i = 0;
// Check permutation elements in second array
while (i < m && result == true)
{
if (store.containsKey(arr2[i]) && store.getValue(arr2[i]) > 0)
{
// When element frequency is more than one
// Reduce its frequency by one
store.put(arr2[i], store.getValue(arr2[i]) - 1);
}
else
{
// In case element not exist in store
result = false;
}
i += 1;
}
}
if (result)
{
print(" Output : Yes \n");
}
else
{
print(" Output : No \n");
}
}
}
fun main(args: Array < String > ): Unit
{
var task: Permutations = Permutations();
// Define arrays of integer elements
var arr1: Array < Int > = arrayOf(8, 5, 7, 9, 4, 7);
var arr2: Array < Int > = arrayOf(9, 5, 7, 7, 9, 8);
var arr3: Array < Int > = arrayOf(9, 7, 5, 7, 8, 4);
// Test cases
task.isPermutations(arr1, arr2);
task.isPermutations(arr1, arr3);
}
Output
Array A : 8 5 7 9 4 7
Array B : 9 5 7 7 9 8
Output : No
Array A : 8 5 7 9 4 7
Array B : 9 7 5 7 8 4
Output : Yes
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment