asvd32

List sum

Jun 5th, 2022
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.02 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4.  
  5. namespace ConsoleApp3
  6. {
  7.     internal class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             int[] array1 = { 1, 2, 3, 5 };
  12.             int[] array2 = { 1, 2, 3, 4, 5 };
  13.             List<int> sumArray = new List<int>();
  14.  
  15.             for (int i = 0; i < array1.Length; i++)
  16.             {
  17.                 sumArray.Add(array1[i]);
  18.             }
  19.             for (int i = 0; i < array2.Length; i++)
  20.             {
  21.                 bool isAdd = true;
  22.  
  23.                 foreach (var element in sumArray)
  24.                 {
  25.                     if (element == array2[i])
  26.                     {
  27.                         isAdd = false;
  28.                     }
  29.                 }
  30.                 if (isAdd)
  31.                 {
  32.                     sumArray.Add(array2[i]);
  33.                 }
  34.             }
  35.             foreach (var elemnt in sumArray)
  36.             {
  37.                 Console.Write(elemnt + " ");
  38.             }
  39.         }
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment