Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two string arrays, one bigger than the other, and i need to check if any of the strings in the arrays are the same.
Posted

My preferred method would be to sort each array.
Then just compare with a single loop:
C#
If (string A > string B)
   move B
else if (string A < string B)
   move A
else 
   Same - record, and move A and B.

You can use Array.Sort to sort string arrays into order:
C#
string[] st = new string[] { "qwertyuiop", "asdfghjkl", "zxcvbnm" };
Array.Sort(st);
 
Share this answer
 
Try this:
C#
string[] array1, array2;
//list to store index's of same strings
List<keyvaluepair><int,int>> couplesList = new List<keyvaluepair><int,int>>();

//array creation goes here

//the algorithm 
for(int i = 0; i < array1.Length; i++)
{
    for(int j = 0; j < array2.Length; j++)
    {
        if(array1[i] == array2[j])
        {
           couplesList.Add(new KeyValuePair<int,int>(i,j));
        }
    }
}</keyvaluepair></keyvaluepair>
 
Share this answer
 
I wud have done something like this:

C#
//smaller is the array with less no. of strings
//bigger is the array with more no. of strings

//this loop compares if any string in smaller is present in bigger
foreach(string i in smaller)
foreach(string j in bigger)
{
if(i==j)
Console.WriteLIne(i);//print the common string
}
 
Share this answer
 
A LINQ solution:
C#
string[] test = new string[] { "one", "two", "three" };
string[] test1 = new string[] { "four", "six", "eight", "ten", "one" };

string[] result = test.Intersect(test1).ToArray();


result contains the strings that are the same in both arrays.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900