Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 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.
- namespace _10.JoinLists
- {
- using System;
- using System.Collections.Generic;
- using System.Linq;
- public class JoinLists
- {
- public static void Main(string[] args)
- {
- checked
- {
- Console.WriteLine("Enter all numbers from the First List on single line, separated by a space!");
- int[] nums1 = ReadStringFromInputAndCreateArrayOfNumbers();
- Console.WriteLine("Enter all numbers from the Second List on single line, separated by a space!");
- int[] nums2 = ReadStringFromInputAndCreateArrayOfNumbers();
- List<int> joinedList = JoinsTwoArraysOfIntegersInOneJoinedList(nums1, nums2);
- joinedList.Sort();
- PrintJoinedListOfNumbers(joinedList);
- }
- }
- private static void PrintJoinedListOfNumbers(List<int> joinedList)
- {
- checked
- {
- Console.WriteLine("The Joined List of numbers is:");
- foreach (int num in joinedList)
- {
- Console.Write("{0} ", num);
- }
- Console.WriteLine();
- }
- }
- private static List<int> JoinsTwoArraysOfIntegersInOneJoinedList(int[] nums1, int[] nums2)
- {
- checked
- {
- List<int> joins = new List<int>();
- for (int i = 0; i < nums1.Length; i++)
- {
- bool numIsInList = joins.Contains(nums1[i]);
- if (!numIsInList)
- {
- joins.Add(nums1[i]);
- }
- }
- for (int j = 0; j < nums2.Length; j++)
- {
- bool numIsInList = joins.Contains(nums2[j]);
- if (!numIsInList)
- {
- joins.Add(nums2[j]);
- }
- }
- return joins;
- }
- }
- private static int[] ReadStringFromInputAndCreateArrayOfNumbers()
- {
- checked
- {
- string inputLine = Console.ReadLine();
- char[] separators = new char[] { ' ', ',', ';' };
- string[] numStr = inputLine.Split(separators, StringSplitOptions.RemoveEmptyEntries);
- int[] nums = new int[numStr.Length];
- for (int i = 0; i < nums.Length; i++)
- {
- nums[i] = int.Parse(numStr[i]);
- }
- return nums;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement