Advertisement
dimipan80

Advanced Topics 10. Join Lists

Jul 3rd, 2014
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.99 KB | None | 0 0
  1. // Write a program that takes as input two lists of integers and joins them. The result should hold all numbers from the first list, and all numbers from the second list, without repeating numbers, and arranged in increasing order. The input and output lists are given as integers, separated by a space, each list at a separate line.
  2.  
  3. namespace _10.JoinLists
  4. {
  5.     using System;
  6.     using System.Collections.Generic;
  7.     using System.Linq;
  8.  
  9.     public class JoinLists
  10.     {
  11.         public static void Main(string[] args)
  12.         {
  13.             checked
  14.             {
  15.                 Console.WriteLine("Enter all numbers from the First List on single line, separated by a space!");
  16.                 int[] nums1 = ReadStringFromInputAndCreateArrayOfNumbers();
  17.                 Console.WriteLine("Enter all numbers from the Second List on single line, separated by a space!");
  18.                 int[] nums2 = ReadStringFromInputAndCreateArrayOfNumbers();
  19.                 List<int> joinedList = JoinsTwoArraysOfIntegersInOneJoinedList(nums1, nums2);
  20.                 joinedList.Sort();
  21.                 PrintJoinedListOfNumbers(joinedList);
  22.             }
  23.         }
  24.  
  25.         private static void PrintJoinedListOfNumbers(List<int> joinedList)
  26.         {
  27.             checked
  28.             {
  29.                 Console.WriteLine("The Joined List of numbers is:");
  30.                 foreach (int num in joinedList)
  31.                 {
  32.                     Console.Write("{0} ", num);
  33.                 }
  34.  
  35.                 Console.WriteLine();
  36.             }
  37.         }
  38.  
  39.         private static List<int> JoinsTwoArraysOfIntegersInOneJoinedList(int[] nums1, int[] nums2)
  40.         {
  41.             checked
  42.             {
  43.                 List<int> joins = new List<int>();                
  44.                 for (int i = 0; i < nums1.Length; i++)
  45.                 {
  46.                     bool numIsInList = joins.Contains(nums1[i]);
  47.                     if (!numIsInList)
  48.                     {
  49.                         joins.Add(nums1[i]);
  50.                     }
  51.                 }
  52.  
  53.                 for (int j = 0; j < nums2.Length; j++)
  54.                 {
  55.                     bool numIsInList = joins.Contains(nums2[j]);
  56.                     if (!numIsInList)
  57.                     {
  58.                         joins.Add(nums2[j]);
  59.                     }
  60.                 }
  61.  
  62.                 return joins;
  63.             }
  64.         }
  65.  
  66.         private static int[] ReadStringFromInputAndCreateArrayOfNumbers()
  67.         {
  68.             checked
  69.             {
  70.                 string inputLine = Console.ReadLine();
  71.  
  72.                 char[] separators = new char[] { ' ', ',', ';' };
  73.                 string[] numStr = inputLine.Split(separators, StringSplitOptions.RemoveEmptyEntries);
  74.                 int[] nums = new int[numStr.Length];
  75.                 for (int i = 0; i < nums.Length; i++)
  76.                 {
  77.                     nums[i] = int.Parse(numStr[i]);
  78.                 }
  79.  
  80.                 return nums;
  81.             }
  82.         }        
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement