Get the value by a comparison between C # arrays

1

Well, I have two arrays:

1st

string[] pastasSistema = new string[]
         {
                "Content",
                "DataRv",
                "My Skype Received Files",
                "RootTools",
                "shared_dynco",
                "shared_httpfe"
         };

2nd

string[] diretorios= new string[]
     {
            "Content",
            "DataRv",
            "My Skype Received Files",
            "Diferente",
            "RootTools",
            "shared_dynco",
            "shared_httpfe"
     };

Where I have the Diferente folder. What I want to do is compare these two arrays, and get the content that is different. In case I want to have the DIFFERENT value from within Array diretorios

    
asked by anonymous 05.10.2016 / 21:11

2 answers

3

You can use Except from Linq, so

string[] diff = diretorios.Except(pastasSistema).ToArray();

Note that Except only takes items from the first array that are not in the second, just as you request

  

In case I am wanting to have the DIFFERENT value from within the Array directories

    
05.10.2016 / 21:23
2

A more trivial approach would be to go through both arrays:

List<string> diferentes = new List<string>();
for (int i = 0; i < diretorios.Length; j++) {
    bool existe = false

    for (int j = 0; j < pastasSistema.Length && !existe; j++) {
        if(diretorios[i].Equals(pastaSistema[j])) existe = true
    }
    if(!existe) diferentes.Add(diretorios[i]);

}

But the language contains simpler methods that do this for you. Look at jbueno's answer

    
05.10.2016 / 21:26