• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 399
  • Last Modified:

comparing arrays c#

Hi:
I have 2 arrays, both string.
I need to store in a third array  those elements that are in array 1 but NOT in array 2.
I have some code that displays which is in BOTH, but that is not what I want!
Does anybody know how to change attached code to show what is in sfolders, but not dfolders?
Thanks!
List<string> miss = new List<string>();

foreach (string txt in dfolders)
{
   if (Array.IndexOf(sfolders, txt) == -1)
   miss.Add(txt);
 }

 

foreach (string item in miss)
{
    System.Diagnostics.Debug.Write(item);
    listBox4.DataSource = miss;
}

Open in new window

0
Richard Teasdale
Asked:
Richard Teasdale
1 Solution
 
Asim NazirCommented:
May be this should work for you:
 
List<string> miss = new List<string>();

foreach (string txt in dfolders)
{
   if (Array.IndexOf(sfolders, txt) >= 0)
   miss.Add(txt);
 }

 

foreach (string item in miss)
{
    System.Diagnostics.Debug.Write(item);
    listBox4.DataSource = miss;
}

Open in new window

0
 
DhaestCommented:
A very easy example provided below
List<string> firstList = new List<string>()
            {
	            "carrot",
	            "apple",
	            "banana"
            };

            List<string> secondList = new List<string>()
            {
	            "carrot",
	            "apple"
            };

            List<string> thirdArray = new List<string>();

            foreach (string item in firstList)
            {
                if (!secondList.Contains(item))
                    thirdArray.Add(item);
            }

Open in new window

0
 
Carl TawnSystems and Integration DeveloperCommented:
Or, if LINQ is an option, then the simpler:
            List<string> firstList = new List<string>()
            {
	            "carrot",
	            "apple",
	            "banana"
            };

            List<string> secondList = new List<string>()
            {
	            "carrot",
	            "apple"
            };

            var unique = (from item in firstList.Except(secondList)
                          select item).ToArray();

            foreach (var item in unique)
                Console.WriteLine(item);

Open in new window

0
 
Richard TeasdaleFinancial ControllerAuthor Commented:
Thank you!
I do not know LINQ so cannot comment on that, but thanks to you all, anyway!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now