Daniel
Daniel

Reputation: 3

C# - TextLogger

My English isn't that good.

I try to make a TextLogger, for example: if I have two different arrays:

string[] array1 = {"a", "b", "c", "d"}
string[] array2 = {"y", "c", "h", "f"}

and I have the char "c" in both of the arrays, then both of the char "c" should be removed.

Output: a, b, d, h, y, f

this is what I managed to do so far:

string[] array1 = {"a", "b", "c", "d"}
string[] array2 = {"y", "c", "h", "f"}
for(int i = 0; i < array1.Length; i++)
{
    if(array1[i] == array2[i])
    {

    }
}

edit(sorry for keep changing my question):

and how I can do it with this:

ArrayList array1 = new ArrayList();
array1.Add("a");
array1.Add("b");
array1.Add("c");
array1.Add("d");

ArrayList array2 = new ArrayList();
array1.Add("y");
array1.Add("c");
array1.Add("h");
array1.Add("f");

Upvotes: 0

Views: 149

Answers (3)

Oxald
Oxald

Reputation: 837

1- Get common items using Enumerable.Intersect

2- replace each array by the same array except common items using Enumerable.Except

string[] array1 = { "a", "b", "c", "d" };
string[] array2 = { "y", "c", "h", "f" };

var intersect = array1.Intersect(array2); // 1

array1 = array1.Except(intersect).ToArray(); //2
array2 = array2.Except(intersect).ToArray(); //2

Edit: to take into account double values as mentioned in the comment:

string[] array1 = { "a", "b", "b", "b", "c", "d" };
string[] array2 = { "y", "b",  "c", "h", "f" };

var grpArray1 = array1.GroupBy(a => a)
                 .Select(grp => new { item = grp.Key, count = grp.Count() });
var grpArray2 = array2.GroupBy(a => a)
                 .Select(grp => new { item = grp.Key, count = grp.Count() });

array1 = grpArray1.Select(a =>
               {
                var bCount = array2.Count(x => x.Equals(a.item));
                return new { item = a.item, finalCount = a.count - bCount };
               })
              .Where(a => a.finalCount > 0)
              .SelectMany(a => Enumerable.Repeat(a.item, a.finalCount))
              .ToArray();

array2 = grpArray2.Select(a =>
              {
               var bCount = array1.Count(x => x.Equals(a.item));
               return new { item = a.item, finalCount = a.count - bCount };
              })
             .Where(a => a.finalCount > 0)
             .SelectMany(a => Enumerable.Repeat(a.item, a.finalCount))
             .ToArray();

Console.WriteLine("-->array1:");
foreach (var item in array1)
    Console.WriteLine(item);

Console.WriteLine("-->array2:");
foreach (var item in array2)
    Console.WriteLine(item);

The results:

-->array1:
a
b
b
d
-->array2:
y
h
f

Upvotes: 3

Tom Mendelson
Tom Mendelson

Reputation: 625

If if understand your problem correctly, you don't need to change that arrays (array1 and array2) but get a result from both of them. so, you can solve your problem using the GroupBy method

            string[] array1 = { "a", "b", "c", "d" };
            string[] array2 = { "y", "c", "h", "f" };

            var filteredArray = array1.Concat(array2).GroupBy(x => x).Where(x => x.Count() == 1).Select(x=>x.Key);

            Console.WriteLine(string.Join(" ", filteredArray));
            Console.ReadLine();

what we can see here, is concat the arrays into 1 list, then group by the chars into groups, and then filter the groups that contain more than 1 element, and in the end revert it into list of chars (instead of list of groups)

Edit:

out of the comment about the duplicated "b" inside each of the arrays, i created a new (with little bit more complexity) that works for your case:

        string[] array1 = { "a", "b", "b", "c", "d" };
        string[] array2 = { "y", "c", "h", "f" };

        var filteredArray = array1.GroupBy(x => x)
            .Concat(array2.GroupBy(x => x))
            .GroupBy(x => x.Key)
            .Where(x => x.Count() == 1).SelectMany(x => x.Key);

        Console.WriteLine(string.Join(" ", filteredArray));
        Console.ReadLine();

whats happen there? we group the arrays each for himself, then concat the groups together, and then we group the groups by their keys, and the filter where each group contain more then 1 inner group, and in the end we select the groups keys (in addition it's promise us there is only 1 instance of each char)

Hope that helps!

Upvotes: 1

Tommaso Belluzzo
Tommaso Belluzzo

Reputation: 23685

The following code snippet should provide you a clear insight about how to perform your task:

String[] array1 = new String[] {"a", "b", "c", "d"};
String[] array2 = new String[] {"y", "c", "h", "f"};

String[] n1 = array1.Where(x => !array2.Contains(x)).ToArray();
String[] n2 = array2.Where(x => !array1.Contains(x)).ToArray();

Console.WriteLine("Array 1");

foreach (String s in n1)
    Console.WriteLine(s);

Console.WriteLine("\nArray 2");

foreach (String s in n2)
    Console.WriteLine(s);

Console.ReadLine();

The output is:

Array 1
a
b
d

Array 2
y
h
f

and can see a working demo by visiting this link. For more information concerning the methods I used in order to accomplish, visit the following links:

Upvotes: 0

Related Questions