...

/

Solution Review: An Array as a Subset of Another Array

Solution Review: An Array as a Subset of Another Array

This review provides a detailed analysis to help you solve the "An Array as a Subset of Another Array" challenge.

Solution: Lookup in a hash table

Press + to interact
using System;
using System.Collections.Generic;
namespace chapter_9
{
class Program
{
static bool isSubset(int [] arr1, int [] arr2, int size1, int size2)
{
if (size2 > size1)
{
return false;
}
HashSet<int> ht = new HashSet<int>();
// ht stores all the values of arr1
for (int i = 0; i < size1; i++)
{
// following the last element of the container
// If key is not present in the unordered_set then insert it
if (!ht.Contains(arr1[i]))
ht.Add(arr1[i]);
}
// loop to check if all elements of arr2 are also in arr1
for (int i = 0; i < size2; i++)
{
// If key is not found condition will return false
// If found it will return iterator to that key
if (!ht.Contains(arr2[i]))
return false;
}
return true;
}
static void Main(string[] args)
{
int []arr1 = { 9, 4, 7, 1, -2, 6, 5 };
int []arr2 = { 7, 1, -2 };
Console.WriteLine(isSubset(arr1, arr2, 7, 3));
return;
}
}
}

The solution is simple when working with the C# HashSet. Simply, iterate over arr1 on line 16. If the ...