12,701,087 members (28,201 online)
Rate this:
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 9-Mar-12 0:00am

Rate this:

## Solution 1

My preferred method would be to sort each array.
Then just compare with a single loop:
```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:
```string[] st = new string[] { "qwertyuiop", "asdfghjkl", "zxcvbnm" };
Array.Sort(st);```
Rate this:

## Solution 2

Try this:
```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])
{
}
}
}</keyvaluepair>```
Rate this:

## Solution 3

I wud have done something like this:

```//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
}

```
Rate this:

## Solution 4

A LINQ solution:
```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.

Top Experts
Last 24hrsThis month
 OriginalGriff 170 Dave Kreskowiak 95 ppolymorphe 80 Mika Wendelius 60 John Simmons / outlaw programmer 60
 OriginalGriff 4,101 Peter Leow 2,333 ppolymorphe 1,962 Mika Wendelius 1,813 Jochen Arndt 1,479