[Solved] Remove Names With Lists in C#

  

3
Topic starter

Write a program that takes as input two lists of names and removes from the first list all names given in the second list.

The input and output lists are given as words, separated by a space, each list at a separate line.

Examples:

remove names with lists - example

1 Answer
2

Here is my answer dude:

using System;
using System.Collections.Generic;
using System.Linq;
 
class RemoveNames
{
    static void Main()
    {
        List<string> firstList = new List<string>();//FIRST LIST OF NAMES
        string[] firstArray = Console.ReadLine().Split();
 
        for (int i = 0; i < firstArray.Length; i++)
        {
            firstList.Add(firstArray[i]);
        }
 
        List<string> secondList = new List<string>();//SECOND LIST OF NAMES
        string[] secondArray = Console.ReadLine().Split();
 
        for (int i = 0; i < secondArray.Length; i++)
        {
            secondList.Add(secondArray[i]);
        }
 
        List<string> allLists = new List<string>();//COMBINED LIST OF 1ST AND 2ND LISTS
        foreach (var VARIABLE in firstList)
        {
            if (secondList.Contains(VARIABLE))
            {
                continue;
            }
            else
            {
                allLists.Add(VARIABLE);
            }
        }
 
        foreach (var VARIABLE in allLists)
        {
            Console.Write("{0} ", VARIABLE);
        }
        Console.WriteLine();
    }
}
Share: