-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.

4
  • 1
    Do you want to know the count or the items that are not present?What have you tried so far? Commented Aug 23, 2012 at 15:59
  • @freebird I am reading one array in foreach and inside that foreach I am reading other array and then comparing each string one by one. Commented Aug 23, 2012 at 16:01
  • I wanted to know what is there in "A" which is not in "B" Commented Aug 23, 2012 at 16:02
  • stackoverflow.com/questions/713341/comparing-arrays-in-c-sharp Commented Aug 23, 2012 at 16:05

7 Answers 7

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;
Sign up to request clarification or add additional context in comments.

3 Comments

'System.Array' does not contain a definition for 'Contains' and no extension method 'Contains' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?)
I believe that requires LINQ too
Edited my answer. It's really hard to remember what was in 2.0. I had to avoid "var" 3 times!
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);
}

Comments

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.

2 Comments

IntersectWith is only .Net 3.5+
@jmh_gr .Net 3.5 and .Net 2.0 share the same DLR, see the first link for more information.
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);
}

Comments

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'
}

Comments

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();
}

Comments

-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;
 }

4 Comments

This isn't what he wants. He wants the set difference, not sequence equality. The two are quite different.
@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.
I thought I edited the answer .. sorry about that I just updated I will review again as well

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.