Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- class IntersectionAndUnion
- {
- static void Main()
- {
- int[] firstArray = {1,2,3};
- Console.Write("First array: ");
- PrintArray(firstArray);
- Console.Write("Second array: ");
- int[] secondArray = { 23, 34, 45, 3};
- PrintArray(secondArray);
- int[] union = CreateUnion(firstArray, secondArray);
- Console.WriteLine("Without repeate numbers: ");
- PrintArray(union);
- Console.WriteLine("Numbers occur in both arrays:");
- CreateIntersetion(firstArray, secondArray);//направо ги принтя в метода
- }
- static void CreateIntersetion(int[] firstArray, int[] secondArray)
- {
- Console.Write("{");
- foreach (int element in firstArray)
- {
- if(Array.IndexOf(secondArray, element) >= 0)
- {
- Console.Write(element + " ");
- }
- }
- Console.Write("}");
- Console.WriteLine();
- }
- static int[] CreateUnion(int[] firstArray, int[] secondArray)
- {
- List<int> union = new List<int>();
- union.AddRange(firstArray);
- foreach (int element in secondArray)
- {
- //if (Array.IndexOf(firstArray, element) == -1)//Може и така да се направи
- //{
- // union.Add(element);
- //}
- if (!union.Contains(element))
- {
- union.Add(element);
- }
- }
- return union.ToArray();
- }
- private static void PrintArray(int[] firstArray)
- {
- Console.Write("{");
- foreach (int elements in firstArray)
- {
- Console.Write(elements + " ");
- }
- Console.Write("}");
- Console.WriteLine();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment