I am preparing for a interview for a junior level c# position. I take two arrays and find the common int between both of them. Let me know if you have any feedback.
using System;
using System.Collections.Generic;
namespace CommonElement
{
class MainClass
{
static void EnterArray1IntoHashSet (int[] array1, HashSet<int> hs){
foreach (int num in array1)
{
if (!hs.Contains (num))
{
hs.Add (num);
}
}
}
static int FindCommonInt(int[] array2, HashSet<int> hs){
foreach (int num in array2)
{
if (hs.Contains (num))
{
return num;
}
}
return -1;
}
public static void Main (string[] args)
{
int[] array1 = new int[7]{ 5, 6, 7, 8, 1, 2, 3 };
int[] array2 = new int[7]{ 13, 22, 3, 45, 67, 73, 85};
HashSet<int> hs = new HashSet<int> ();
EnterArray1IntoHashSet (array1, hs);
int commonElement = FindCommonInt(array2, hs);
if (commonElement != -1)
{
Console.WriteLine (commonElement);
}
else
{
Console.WriteLine ("No Common Element");
}
}
}
}