-2

Possible Duplicate:
Comparing Arrays in C#

I have a two string arrays:

string[] a;
string[] b;

How can I identify how many (and what) items of a are not present in b? As I am using .NET 2.0 so I can't use linq.

Community
  • 1
  • 1
Zerotoinfinity
  • 6,290
  • 32
  • 130
  • 206

7 Answers7

2
List<string> result = new List<string>();
foreach (string sa in a)
{
   if (Array.IndexOf(b, sa) < 0)
      result.Add(sa);
}

int count = result.Count;
Amiram Korach
  • 13,056
  • 3
  • 28
  • 30
1

convert them both to a List the do something like this:

List<string> difference = new List<string>();
foreach(string word in a)
{
    if(!b.Contains(word))
        difference.Add(word);
}
Nicolas Brown
  • 1,546
  • 1
  • 10
  • 17
1

I would recommand to transform your arrays of strings into HashSet<T>s.
See here for how to use a HashSet<T> in .NET 2.0

Then

How can I identify how many (and what) items of a are not present in b?

--> IntersectWith does precisely that.

Community
  • 1
  • 1
Louis Kottmann
  • 16,268
  • 4
  • 64
  • 88
1

Try this:

string[] a = ...;
string[] b = ...;

List<string> bList = new List<string>(b);
List<string> valuesInAButNotInB = new List<string>();
foreach (string value in a)
{
    if (!bList.Contains(value))
        valuesInAButNotInB.Add(value);
}
Dan
  • 9,717
  • 4
  • 47
  • 65
1

What you need to do is store the items from one list in a set, and then remove all of the items from that set if they are in the other collection. This will be much quicker for larger data sets than two nested loops, or performing lots of linear searches on one of the arrays.

Since HashSet doesn't exist in 2.0 I just use a Dictionary and ignore the values. It's a hack, but not a terrible one at that.

string[] a = null;
string[] b = null;
Dictionary<string, string> values = new Dictionary<string, string>();
foreach (string s in a)
{
    values.Add(s, s);
}

foreach (string s in b)
{
    values.Remove(s);
}

foreach (string s in values.Keys)
{
    Console.WriteLine(s);//This string is in 'a' and not in 'b'
}
Servy
  • 202,030
  • 26
  • 332
  • 449
0

Just enumerate items in both a and b, just like in the old days:

private static void Main(string[] args)
{
    string[] a = new string[] { "a", "b", "c", "d" };
    string[] b = new string[] { "c", "d" };

    foreach (string tmp in a)
    {
        bool existsInB = false;
        foreach (string tmp2 in b)
        {
            if (tmp == tmp2)
            {
                existsInB = true;
                break;
            }
        }

        if (!existsInB)
        {
            Console.WriteLine(string.Format("{0} is not in b", tmp));
        }
    }

    Console.ReadLine();
}
ken2k
  • 48,145
  • 10
  • 116
  • 176
-1
private List<string> CompareArray(string[]  arr1, string[] arr2)
{
        List<string> compareList = new List<string>();
        //iterate throught it
        foreach( string str in arr1 )
        {
            if(!arr2.Contains( str ))
            {
                compareList.Add(str);
            }
        }
            return compareList;
 }
MethodMan
  • 18,625
  • 6
  • 34
  • 52
  • http://stackoverflow.com/questions/713341/comparing-arrays-in-c-sharp – reggie Aug 23 '12 at 16:04
  • 1
    This isn't what he wants. He wants the set difference, not sequence equality. The two are quite different. – Servy Aug 23 '12 at 16:15
  • @DJKRAZE You've changed it to now be set equality, rather than sequence equality (it doesn't require the same ordering) but it's still not a set difference. It needs to return the set of items in `a` that are not in `b`. That won't be a boolean value. The return type of your method alone tells you this isn't right. – Servy Aug 23 '12 at 16:38
  • I thought I edited the answer .. sorry about that I just updated I will review again as well – MethodMan Aug 23 '12 at 16:42