Check if array is subset of another array
Here given code implementation process.
// C++ program
// Check if array is subset of another array
// Using STL
#include <iostream>
#include <algorithm>
#include <set>
using namespace std;
//This function are displaying given array elements
void display(int arr[], int size)
{
cout << " Array : ";
for (int i = 0; i < size; ++i)
{
cout << " " << arr[i];
}
cout << endl;
}
//Check that whether given arrays is form of subset or not
int find_subset(int first[], int second[], int s1, int s2)
{
//Create a set which are contained a integer value
set < int > s;
for (int i = 0; i < s1; ++i)
{
//get unique values of first array
s.insert(first[i]);
}
display(first, s1);
display(second, s2);
for (int i = 0; i < s2; ++i)
{
if (find(s.begin(), s.end(), second[i]) == s.end())
{
//When element not exist in first array
return false;
}
}
return true;
}
void check_sub_set(int first[], int second[], int s1, int s2)
{
bool status = false;
//Compare the size of given array
if (s1 > s2)
{
//When given first array size are greater than second
status = find_subset(first, second, s1, s2);
}
else
{
status = find_subset(second, first, s2, s1);
}
if (status == true)
{
cout << " Subset are exist\n" << endl;
}
else
{
cout << " Subset are not exist\n" << endl;
}
}
int main()
{
//Define array elements
int arr1[] = {
7 , 1 , 0 , 9 , 6 , 5 , 3 , 4 , 5 , 8 , 3
};
int arr2[] = {
4 , 1 , 6
};
int arr3[] = {
4 , 1 , 6 , 2
};
//Get the size
int size1 = sizeof(arr1) / sizeof(arr1[0]);
int size2 = sizeof(arr2) / sizeof(arr2[0]);
int size3 = sizeof(arr3) / sizeof(arr3[0]);
check_sub_set(arr1, arr2, size1, size2);
check_sub_set(arr1, arr3, size1, size3);
return 0;
}
Output
Array : 7 1 0 9 6 5 3 4 5 8 3
Array : 4 1 6
Subset are exist
Array : 7 1 0 9 6 5 3 4 5 8 3
Array : 4 1 6 2
Subset are not exist
/*
Java program
Check if array is subset of another array
*/
import java.util.HashSet;
public class SubSet
{
//This function are displaying given array elements
public void display(int[] arr, int size)
{
System.out.print(" Array : ");
for (int i = 0; i < size; ++i)
{
System.out.print(" " + arr[i]);
}
System.out.print("\n");
}
//Check that whether given arrays is form of subset or not
public boolean findSubSet(int[] first, int[] second, int s1, int s2)
{
// Use to check subset
HashSet <Integer> result = new HashSet <> ();
for (int i = 0; i < s1; ++i)
{
// Get unique values of first array
result.add(first[i]);
}
display(first, s1);
display(second, s2);
for (int i = 0; i < s2; ++i)
{
if (result.contains(second[i])==false)
{
//When element not exist in first array
return false;
}
}
return true;
}
// Handles the request to finding subset exist in given two array
public void checkSubset(int[] first, int[] second)
{
// Get the size
int s1 = first.length;
int s2 = second.length;
boolean status = false;
//Compare the size of given array
if (s1 > s2)
{
//When given first array size are greater than second
status = findSubSet(first, second, s1, s2);
}
else
{
status = findSubSet(second, first, s2, s1);
}
if (status == true)
{
System.out.print(" Subset are exist\n");
}
else
{
System.out.print(" Subset are not exist\n");
}
}
public static void main(String[] args)
{
SubSet task = new SubSet();
//Define array elements
int[] arr1 = {
7 , 1 , 0 , 9 , 6 , 5 , 3 , 4 , 5 , 8 , 3
};
int[] arr2 = {
4 , 1 , 6
};
int[] arr3 = {
4 , 1 , 6 , 2
};
task.checkSubset(arr1, arr2);
task.checkSubset(arr1, arr3);
}
}
Output
Array : 7 1 0 9 6 5 3 4 5 8 3
Array : 4 1 6
Subset are exist
Array : 7 1 0 9 6 5 3 4 5 8 3
Array : 4 1 6 2
Subset are not exist
// Include namespace system
using System;
using System.Collections.Generic;
public class SubSet
{
//This function are displaying given array elements
public void display(int[] arr, int size)
{
Console.Write(" Array : ");
for (int i = 0; i < size; ++i)
{
Console.Write(" " + arr[i]);
}
Console.Write("\n");
}
//Check that whether given arrays is form of subset or not
public Boolean findSubSet(int[] first, int[] second, int s1, int s2)
{
// Use to check subset
HashSet < int > result = new HashSet <int> ();
for (int i = 0; i < s1; ++i)
{
result.Add(first[i]);
}
display(first, s1);
display(second, s2);
for (int i = 0; i < s2; ++i)
{
if (result.Contains(second[i]) == false)
{
//When element not exist in first array
return false;
}
}
return true;
}
// Handles the request to finding subset exist in given two array
public void checkSubset(int[] first, int[] second)
{
// Get the size
int s1 = first.Length;
int s2 = second.Length;
Boolean status = false;
//Compare the size of given array
if (s1 > s2)
{
//When given first array size are greater than second
status = findSubSet(first, second, s1, s2);
}
else
{
status = findSubSet(second, first, s2, s1);
}
if (status == true)
{
Console.Write(" Subset are exist\n");
}
else
{
Console.Write(" Subset are not exist\n");
}
}
public static void Main(String[] args)
{
SubSet task = new SubSet();
//Define array elements
int[] arr1 = {
7 , 1 , 0 , 9 , 6 , 5 , 3 , 4 , 5 , 8 , 3
};
int[] arr2 = {
4 , 1 , 6
};
int[] arr3 = {
4 , 1 , 6 , 2
};
task.checkSubset(arr1, arr2);
task.checkSubset(arr1, arr3);
}
}
Output
Array : 7 1 0 9 6 5 3 4 5 8 3
Array : 4 1 6
Subset are exist
Array : 7 1 0 9 6 5 3 4 5 8 3
Array : 4 1 6 2
Subset are not exist
<?php
/*
Php program
Check if array is subset of another array
*/
class SubSet
{
//This function are displaying given array elements
public function display( & $arr, $size)
{
echo " Array : ";
for ($i = 0; $i < $size; ++$i)
{
echo " ". $arr[$i];
}
echo "\n";
}
//Check that whether given arrays is form of subset or not
public function findSubSet( $first, $second, $s1, $s2)
{
// Use to check subset
$result = array();
for ($i = 0; $i < $s1; ++$i)
{
if (in_array($first[$i], $result,True) == false)
{
// Get unique values of first array
$result[]=$first[$i];
}
}
$this->display($first, $s1);
$this->display($second, $s2);
for ($i = 0; $i < $s2; ++$i)
{
if (in_array($second[$i], $result,True) == false)
{
//When element not exist in first array
return false;
}
}
return true;
}
// Handles the request to finding subset exist in given two array
public function checkSubset( $first, $second)
{
// Get the size
$s1 = count($first);
$s2 = count($second);
$status = false;
//Compare the size of given array
if ($s1 > $s2)
{
//When given first array size are greater than second
$status = $this->findSubSet($first, $second, $s1, $s2);
}
else
{
$status = $this->findSubSet($second, $first, $s2, $s1);
}
if ($status == true)
{
echo " Subset are exist\n";
}
else
{
echo " Subset are not exist\n";
}
}
}
function main()
{
$task = new SubSet();
//Define array elements
$arr1 = array(7, 1, 0, 9, 6, 5, 3, 4, 5, 8, 3);
$arr2 = array(4, 1, 6);
$arr3 = array(4, 1, 6, 2);
$task->checkSubset($arr1, $arr2);
$task->checkSubset($arr1, $arr3);
}
main();
Output
Array : 7 1 0 9 6 5 3 4 5 8 3
Array : 4 1 6
Subset are exist
Array : 7 1 0 9 6 5 3 4 5 8 3
Array : 4 1 6 2
Subset are not exist
/*
Node Js program
Check if array is subset of another array
*/
class SubSet
{
//This function are displaying given array elements
display(arr, size)
{
process.stdout.write(" Array : ");
for (var i = 0; i < size; ++i)
{
process.stdout.write(" " + arr[i]);
}
process.stdout.write("\n");
}
//Check that whether given arrays is form of subset or not
findSubSet(first, second, s1, s2)
{
// Use to check subset
var result = new Set();
for (var i = 0; i < s1; ++i)
{
result.add(first[i]);
}
this.display(first, s1);
this.display(second, s2);
for (var i = 0; i < s2; ++i)
{
if (result.has(second[i]) == false)
{
//When element not exist in first array
return false;
}
}
return true;
}
// Handles the request to finding subset exist in given two array
checkSubset(first, second)
{
// Get the size
var s1 = first.length;
var s2 = second.length;
var status = false;
//Compare the size of given array
if (s1 > s2)
{
//When given first array size are greater than second
status = this.findSubSet(first, second, s1, s2);
}
else
{
status = this.findSubSet(second, first, s2, s1);
}
if (status == true)
{
process.stdout.write(" Subset are exist\n");
}
else
{
process.stdout.write(" Subset are not exist\n");
}
}
}
function main()
{
var task = new SubSet();
//Define array elements
var arr1 = [7, 1, 0, 9, 6, 5, 3, 4, 5, 8, 3];
var arr2 = [4, 1, 6];
var arr3 = [4, 1, 6, 2];
task.checkSubset(arr1, arr2);
task.checkSubset(arr1, arr3);
}
main();
Output
Array : 7 1 0 9 6 5 3 4 5 8 3
Array : 4 1 6
Subset are exist
Array : 7 1 0 9 6 5 3 4 5 8 3
Array : 4 1 6 2
Subset are not exist
# Python 3 program
# Check if array is subset of another array
class SubSet :
# This function are displaying given list elements
def display(self, arr, size) :
print(" Array : ", end = "")
i = 0
while (i < size) :
print(" ", arr[i], end = "")
i += 1
print(end = "\n")
# Check that whether given lists is form of subset or not
def findSubSet(self, first, second, s1, s2) :
# Use to check subset
result = set()
i = 0
while (i < s1) :
# Get unique values of first list
result.add(first[i])
i += 1
self.display(first, s1)
self.display(second, s2)
i = 0
while (i < s2) :
if (second[i] in result) == False :
# When element not exist in first list
return False
i += 1
return True
# Handles the request to finding subset exist in given two list
def checkSubset(self, first, second) :
# Get the size
s1 = len(first)
s2 = len(second)
status = False
# Compare the size of given list
if (s1 > s2) :
# When given first list size are greater than second
status = self.findSubSet(first, second, s1, s2)
else :
status = self.findSubSet(second, first, s2, s1)
if (status == True) :
print(" Subset are exist")
else :
print(" Subset are not exist")
def main() :
task = SubSet()
# Define list elements
arr1 = [7, 1, 0, 9, 6, 5, 3, 4, 5, 8, 3]
arr2 = [4, 1, 6]
arr3 = [4, 1, 6, 2]
task.checkSubset(arr1, arr2)
task.checkSubset(arr1, arr3)
if __name__ == "__main__": main()
Output
Array : 7 1 0 9 6 5 3 4 5 8 3
Array : 4 1 6
Subset are exist
Array : 7 1 0 9 6 5 3 4 5 8 3
Array : 4 1 6 2
Subset are not exist
# Ruby program
# Check if array is subset of another array
class SubSet
# This function are displaying given array elements
def display(arr, size)
print(" Array : ")
i = 0
while (i < size)
print(" ", arr[i])
i += 1
end
print("\n")
end
# Check that whether given arrays is form of subset or not
def findSubSet(first, second, s1, s2)
# Use to check subset
result = []
i = 0
while (i < s1)
result.push(first[i])
i += 1
end
self.display(first, s1)
self.display(second, s2)
i = 0
while (i < s2)
if (result.include?(second[i]) == false)
# When element not exist in first array
return false
end
i += 1
end
return true
end
# Handles the request to finding subset exist in given two array
def checkSubset(first, second)
# Get the size
s1 = first.length
s2 = second.length
status = false
# Compare the size of given array
if (s1 > s2)
# When given first array size are greater than second
status = self.findSubSet(first, second, s1, s2)
else
status = self.findSubSet(second, first, s2, s1)
end
if (status == true)
print(" Subset are exist\n")
else
print(" Subset are not exist\n")
end
end
end
def main()
task = SubSet.new()
# Define array elements
arr1 = [7, 1, 0, 9, 6, 5, 3, 4, 5, 8, 3]
arr2 = [4, 1, 6]
arr3 = [4, 1, 6, 2]
task.checkSubset(arr1, arr2)
task.checkSubset(arr1, arr3)
end
main()
Output
Array : 7 1 0 9 6 5 3 4 5 8 3
Array : 4 1 6
Subset are exist
Array : 7 1 0 9 6 5 3 4 5 8 3
Array : 4 1 6 2
Subset are not exist
import scala.collection.mutable._;
/*
Scala program
Check if array is subset of another array
*/
class SubSet
{
//This function are displaying given array elements
def display(arr: Array[Int], size: Int): Unit = {
print(" Array : ");
var i: Int = 0;
while (i < size)
{
print(" " + arr(i));
i += 1;
}
print("\n");
}
//Check that whether given arrays is form of subset or not
def findSubSet(first: Array[Int], second: Array[Int], s1: Int, s2: Int): Boolean = {
// Use to check subset
var result: Set[Int] = Set();
var i: Int = 0;
while (i < s1)
{
result.add(first(i));
i += 1;
}
this.display(first, s1);
this.display(second, s2);
i = 0;
while (i < s2)
{
if (result.contains(second(i)) == false)
{
//When element not exist in first array
return false;
}
i += 1;
}
return true;
}
// Handles the request to finding subset exist in given two array
def checkSubset(first: Array[Int], second: Array[Int]): Unit = {
// Get the size
var s1: Int = first.length;
var s2: Int = second.length;
var status: Boolean = false;
//Compare the size of given array
if (s1 > s2)
{
//When given first array size are greater than second
status = this.findSubSet(first, second, s1, s2);
}
else
{
status = this.findSubSet(second, first, s2, s1);
}
if (status == true)
{
print(" Subset are exist\n");
}
else
{
print(" Subset are not exist\n");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: SubSet = new SubSet();
//Define array elements
var arr1: Array[Int] = Array(7, 1, 0, 9, 6, 5, 3, 4, 5, 8, 3);
var arr2: Array[Int] = Array(4, 1, 6);
var arr3: Array[Int] = Array(4, 1, 6, 2);
task.checkSubset(arr1, arr2);
task.checkSubset(arr1, arr3);
}
}
Output
Array : 7 1 0 9 6 5 3 4 5 8 3
Array : 4 1 6
Subset are exist
Array : 7 1 0 9 6 5 3 4 5 8 3
Array : 4 1 6 2
Subset are not exist
/*
Swift 4 program
Check if array is subset of another array
*/
class SubSet
{
//This function are displaying given array elements
func display(_ arr: [Int], _ size: Int)
{
print(" Array : ", terminator: "");
var i: Int = 0;
while (i < size)
{
print(" ", arr[i], terminator: "");
i += 1;
}
print(terminator: "\n");
}
//Check that whether given arrays is form of subset or not
func findSubSet(_ first: [Int], _ second: [Int], _ s1: Int, _ s2: Int)->Bool
{
// Use to check subset
var result = Set<Int>()
var i: Int = 0;
while (i < s1)
{
result.insert(first[i]);
i += 1;
}
self.display(first, s1);
self.display(second, s2);
i = 0;
while (i < s2)
{
if (result.contains(second[i]) == false)
{
//When element not exist in first array
return false;
}
i += 1;
}
return true;
}
// Handles the request to finding subset exist in given two array
func checkSubset(_ first: [Int], _ second: [Int])
{
// Get the size
let s1: Int = first.count;
let s2: Int = second.count;
var status: Bool = false;
//Compare the size of given array
if (s1 > s2)
{
//When given first array size are greater than second
status = self.findSubSet(first, second, s1, s2);
}
else
{
status = self.findSubSet(second, first, s2, s1);
}
if (status == true)
{
print(" Subset are exist");
}
else
{
print(" Subset are not exist");
}
}
}
func main()
{
let task: SubSet = SubSet();
//Define array elements
let arr1: [Int] = [7, 1, 0, 9, 6, 5, 3, 4, 5, 8, 3];
let arr2: [Int] = [4, 1, 6];
let arr3: [Int] = [4, 1, 6, 2];
task.checkSubset(arr1, arr2);
task.checkSubset(arr1, arr3);
}
main();
Output
Array : 7 1 0 9 6 5 3 4 5 8 3
Array : 4 1 6
Subset are exist
Array : 7 1 0 9 6 5 3 4 5 8 3
Array : 4 1 6 2
Subset are not exist
/*
Kotlin program
Check if array is subset of another array
*/
class SubSet
{
//This function are displaying given array elements
fun display(arr: Array < Int > , size: Int): Unit
{
print(" Array : ");
var i: Int = 0;
while (i < size)
{
print(" " + arr[i]);
i += 1;
}
print("\n");
}
//Check that whether given arrays is form of subset or not
fun findSubSet(first: Array < Int > , second: Array <Int> , s1: Int, s2: Int): Boolean
{
// Use to check subset
var result: MutableSet < Int > = mutableSetOf < Int > ();
var i: Int = 0;
while (i < s1)
{
result.add(first[i]);
i += 1;
}
this.display(first, s1);
this.display(second, s2);
i = 0;
while (i < s2)
{
if (result.contains(second[i]) == false)
{
//When element not exist in first array
return false;
}
i += 1;
}
return true;
}
// Handles the request to finding subset exist in given two array
fun checkSubset(first: Array < Int > , second: Array < Int > ): Unit
{
// Get the size
var s1: Int = first.count();
var s2: Int = second.count();
var status: Boolean ;
//Compare the size of given array
if (s1 > s2)
{
//When given first array size are greater than second
status = this.findSubSet(first, second, s1, s2);
}
else
{
status = this.findSubSet(second, first, s2, s1);
}
if (status == true)
{
print(" Subset are exist\n");
}
else
{
print(" Subset are not exist\n");
}
}
}
fun main(args: Array < String > ): Unit
{
var task: SubSet = SubSet();
//Define array elements
var arr1: Array < Int > = arrayOf(7, 1, 0, 9, 6, 5, 3, 4, 5, 8, 3);
var arr2: Array < Int > = arrayOf(4, 1, 6);
var arr3: Array < Int > = arrayOf(4, 1, 6, 2);
task.checkSubset(arr1, arr2);
task.checkSubset(arr1, arr3);
}
Output
Array : 7 1 0 9 6 5 3 4 5 8 3
Array : 4 1 6
Subset are exist
Array : 7 1 0 9 6 5 3 4 5 8 3
Array : 4 1 6 2
Subset are not exist
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