using System.Collections.Generic;
private static List<int> getCommonElem(Dictionary<long, long> atmpDict, Dictionary<long, long> btmpDict){
List<int> resultantCommonElem = new List<int>();
int aDictCount = atmpDict.Count();
int bDictCount = btmpDict.Count();
Console.WriteLine("Length of Dictionary A and B is {0} and {1}", aDictCount, bDictCount);
for(var j = 0; j < aDictCount; j++){
if(btmpDict.ContainsKey(atmpDict.ElementAt(j).Key)){
long curr_ADictValue = atmpDict.ElementAt(j).Value;
long curr_BDictValue = btmpDict[atmpDict.ElementAt(j).Key];
long min_DictValue = (curr_ADictValue <= curr_BDictValue) ? curr_ADictValue : curr_BDictValue;
for(var k = 0; k < min_DictValue; k++){
resultantCommonElem.Add((int)atmpDict.ElementAt(j).Key);
return resultantCommonElem;
private static List<int> Solve_CommElem(List<int> A, List<int> B)
Dictionary<long, long> aDict = new Dictionary<long, long>();
Dictionary<long, long> bDict = new Dictionary<long, long>();
List<int> resultantCommonElem = new List<int>();
int aCount = A.Count - 1; int bCount = B.Count - 1;
while(aCount >= 0 || bCount >= 0)
if(aDict.ContainsKey(A[aCount])) {
long tmp = aDict[A[aCount]] + 1;
if(bDict.ContainsKey(B[bCount])) {
long tmp = bDict[B[bCount]] + 1;
resultantCommonElem = Program.getCommonElem(aDict, bDict);
return resultantCommonElem;
public static void Main()
List<int> A = new List<int>() { 8, 15 };
List<int> B = new List<int>() { 10 };
List<int> commElem = Program.Solve_CommElem(A, B);
Console.WriteLine("Common Elements are: " + string.Join(" ", commElem));