Intersection function in C# (updated)

C# lacks math functions. Recently I needed to use intersection of strings in my project. It looks like a fairly common and simple task but quick Google research didn’t bring any valuable results. Just to refresh your memory, Intersection is a common set of elements from two or more collections. Wiki: http://en.wikipedia.org/wiki/Intersection_%28set_theory%29

So, here are two versions of Intersection function written in C#. First one takes two string arrays as parameters and returns an intersection of them.

public static string[] Intersection(string[] arrayA, string[] arrayB)
{
        ArrayList outArray = new ArrayList();
        for (int i = 0; i < arrayA.Length; i++)
        {
                for (int j = 0; j < arrayB.Length; j++)
                {
                        if (arrayA[i] == arrayB[j] && outArray.IndexOf(arrayB[j]) < 0)
                                outArray.Add(arrayB[j]);
                }
        }
        return (String[])outArray.ToArray(typeof(string));
}

Second one takes a Jagged Array of strings as a parameter and returns an intersection of them.

public static string[] Intersection(string[][] jArray)
{
        string[] resArray = new string[] { };
        int nArrays = jArray.Length;
       
        if (nArrays > 0)
        {
                //We are testing against the first array
                string[] testArray = jArray[0];
                if (nArrays > 1)                       
                {
                        ArrayList outArray = new ArrayList();

                        //Loop thru elemets in test array
                        for (int n = 0; n < testArray.Length; n++)
                        {
                                //Init counter
                                int resFound = 0; 

                                //Loop thru all elements in jagged array 
                                for (int i = 1; i < nArrays; i++)
                                {
                                        for (int j = 0; j < jArray[i].Length; j++)
                                        {
                                                if (jArray[i][j] == testArray[n])
                                                {
                                                        //Match found - increment the counter
                                                        //and break to the next array
                                                        resFound++;
                                                        break;
                                                }
                                        }
                                }

                                //Check if number of matches is equal or greater than number of arrays
                                if (resFound >= nArrays - 1 && outArray.IndexOf(testArray[n]) < 0)
                                        outArray.Add(testArray[n]);
                        }

                        resArray = (String[])outArray.ToArray(typeof(string));
                }
                else                
                        //Use whole test array if it is the only one
                        resArray = testArray;
               
        }
        return resArray;
}

 

 

posted on Thursday, September 14, 2006 4:44 PM by alexark

Comments