Advertisement
coasterka

EqualArrElementsIntoNewArr

Mar 11th, 2014
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.78 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace EqualArrElementsIntoNewArr
  6. {
  7.     class EqualArrElementsIntoNewArr
  8.     {
  9.         static void Main()
  10.         {
  11.             Console.WriteLine("Input length of first array.");
  12.             int n = int.Parse(Console.ReadLine());
  13.             Console.WriteLine("Input length of second array.");
  14.             int m = int.Parse(Console.ReadLine());
  15.             int[] firstArr = new int[n];
  16.             int[] secondArr = new int[m];
  17.             for (int i = 0; i < n; i++)
  18.             {
  19.                 Console.WriteLine("First array, Element {0}", (i+1));
  20.                 firstArr[i] = int.Parse(Console.ReadLine());
  21.             }
  22.             for (int i = 0; i < m; i++)
  23.             {
  24.                 Console.WriteLine("Second array, Element {0}", (i+1));
  25.                 secondArr[i] = int.Parse(Console.ReadLine());
  26.             }
  27.             for (int i = 0; i < n; i++)
  28.             { Console.Write(firstArr[i] + " "); }
  29.             Console.WriteLine();
  30.             for (int i = 0; i < m; i++)
  31.             { Console.Write(secondArr[i] + " "); }
  32.             Console.WriteLine();
  33.             Console.WriteLine("Equal elements: ");
  34.             Console.WriteLine();
  35.             IEnumerable<int> en = firstArr.Intersect(secondArr);
  36.             int[] thirdArr = en.ToArray();
  37.             int max = thirdArr[0];
  38.             for (int i = 0; i < thirdArr.Length; i++)
  39.             {
  40.                 Console.Write(thirdArr[i] + " ");
  41.                 if (thirdArr[i] > max)
  42.                 {
  43.                     max = thirdArr[i];
  44.                 }
  45.             }
  46.             Console.WriteLine();
  47.             Console.WriteLine("The maximum value from the equal elements is: {0}", max);            
  48.         }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement