OneCompiler

Common element in 3 sorted arrays

180
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace OneCompiler
{
	public class Program
	{
		public static void Main(string[] args)
		{
			int m = int.Parse(Console.ReadLine());
			
			int[] arr1 = Array.ConvertAll(Console.ReadLine().Split(" "), new Converter<string,int>(StringToInteger));
			
			int n = int.Parse(Console.ReadLine());
			
			int[] arr2 = Array.ConvertAll(Console.ReadLine().Split(" "), new Converter<string,int>(StringToInteger));
			
			int o = int.Parse(Console.ReadLine());
			
			int[] arr3 = Array.ConvertAll(Console.ReadLine().Split(" "), new Converter<string,int>(StringToInteger));
			
			var intersection = Intersection(arr1, arr2, arr3);
			
			for (int i = 0; i < intersection.Length; i++)
			{
			  Console.Write(intersection[i] + " ");
			}
		}
		
		private static int StringToInteger(string s)
		{
		  return int.Parse(s);
		}
		
		private static int[] Intersection(int[] arr1, int[] arr2, int[] arr3)
		{
		  int m = arr1.Length;
		  int n = arr2.Length;
		  int o = arr3.Length;
		  
		  var i = 0;
		  var j = 0;
		  var k = 0;
		  
		  var hashSet = new HashSet<int>();
		  
		  while (i < m && j < n && k < o)
		  {
		    if (arr1[i] == arr2[j] && arr2[j] == arr3[k])
		    {
		      hashSet.Add(arr1[i]);
		      i++;
		      j++;
		      k++;
		    }
		    else if (arr1[i] < arr2[j])
		    {
		      i++;
		    }
		    else if (arr2[j] < arr3[k])
		    {
		      j++;
		    }
		    else
		    {
		      k++;
		    }
		  }
		  
		  return hashSet.ToArray();
		}
	}
}