none
string Array performance RRS feed

  • Question

  • In a method I am using following code.

            string[] Array1 = new string[] { "Orange","Mango","welcome","power","Street","Visual"};

            string[] Array2 = new string[] { "Orange", "Mango", "welcome"};

            string[] Array3 = new string[] { "Orange", "Mango",  "power", "Street" };

            string[] Array4 = new string[] { "Orange", "welcome", "power", "Street" };

    I have total 4 arrays some items are repeating in the array. I want to take only the common items from all the array.

    What is the best technique to improve the performance.

    Tuesday, June 11, 2013 6:49 PM

Answers

  • You could use Intersect to do this:

    var shared = Array1.Intersect(Array2).Intersect(Array3).Intersect(Array4);
    


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Tuesday, June 11, 2013 7:02 PM
    Moderator
  • If performance is absolutely critical, I believe manually using a HashSet<T> would be faster:

    var hash = new HashSet<string>(Array1);
    var hash2 = new HashSet<string>();
    
    foreach(var v in Array2)
    {
        if (hash.Remove(v))
            hash2.Add(v);
    }
    
    hash.Clear();
    
    foreach(var v in Array3)
    {
        if (hash2.Remove(v))
            hash.Add(v);
    }
    
    hash2.Clear();
    
    foreach(var v in Array4)
    {
        if (hash.Remove(v))
            hash2.Add(v);
    }
    
    // hash2  will contain your intersection results

    This is effectively what Intersect does, but it's reusing the types instead of making new hashes each time, which should add a (very slight) improvement at the cost of a lot of readability.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Tuesday, June 11, 2013 7:28 PM
    Moderator

All replies

  • You could use Intersect to do this:

    var shared = Array1.Intersect(Array2).Intersect(Array3).Intersect(Array4);
    


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Tuesday, June 11, 2013 7:02 PM
    Moderator
  • If performance is absolutely critical, I believe manually using a HashSet<T> would be faster:

    var hash = new HashSet<string>(Array1);
    var hash2 = new HashSet<string>();
    
    foreach(var v in Array2)
    {
        if (hash.Remove(v))
            hash2.Add(v);
    }
    
    hash.Clear();
    
    foreach(var v in Array3)
    {
        if (hash2.Remove(v))
            hash.Add(v);
    }
    
    hash2.Clear();
    
    foreach(var v in Array4)
    {
        if (hash.Remove(v))
            hash2.Add(v);
    }
    
    // hash2  will contain your intersection results

    This is effectively what Intersect does, but it's reusing the types instead of making new hashes each time, which should add a (very slight) improvement at the cost of a lot of readability.


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Tuesday, June 11, 2013 7:28 PM
    Moderator