Compare 2 arrays

1. Junior Member
Join Date
Dec 1969
Posts
11

## Compare 2 arrays

Hi, I want to compare two arrays to see if they contain any common elements. Is there an easy way to do that?<BR><BR>Thanks!

2. Senior Member
Join Date
Dec 1969
Posts
2,243

## RE: Compare 2 arrays

What type of array?

3. Junior Member
Join Date
Dec 1969
Posts
11

## RE: Compare 2 arrays

I will have 2 lists of numbers to compare. For example, "1,2,3,4,5" vs. "3,6,7" I just want to know if there is any number common to both lists, but I don&#039;t really care what that common number is.

4. Senior Member
Join Date
Dec 1969
Posts
2,243

## RE: Compare 2 arrays

[code language="C#"]<BR>private void Page_Load(object sender, System.EventArgs e)<BR> {<BR> int[] ary1 = new int[10]{1,2,3,4,5,6,7,8,9,10};<BR> int[] ary2 = new int[5]{11,123,3,9,5467};<BR> bool dupes = hasDupes(ary1, ary2);<BR> Response.Write("It is " + dupes.ToString() + " that the arrays containe dupes");<BR> }<BR><BR> private bool hasDupes(int[] ary1, int[] ary2)<BR> {<BR> int i, j;<BR> bool foundDupe = false;<BR><BR> for(i = 0; i &#060; ary1.Length; i++)<BR> {<BR> for(j = 0; j &#060; ary2.Length; j++)<BR> {<BR> if(ary1[i] == ary2[j])<BR> {<BR> foundDupe = true;<BR> break;<BR> }<BR> }<BR> if(foundDupe)<BR> break;<BR> }<BR> return foundDupe;<BR> }<BR>[/code]

5. Senior Member
Join Date
Dec 1969
Posts
2,243

## RE: Grrrrr.....

I mess up the formatting every time!!!<BR><BR>[code language="C#"]<BR>private void Page_Load(object sender, System.EventArgs e)<BR> {<BR> int[] ary1 = new int[10]{1,2,3,4,5,6,7,8,9,10};<BR> int[] ary2 = new int[5]{11,123,3,9,5467};<BR> bool dupes = hasDupes(ary1, ary2);<BR> Response.Write("It is " + dupes.ToString() + " that the arrays containe dupes");<BR> }<BR><BR> private bool hasDupes(int[] ary1, int[] ary2)<BR> {<BR> int i, j;<BR> bool foundDupe = false;<BR><BR> for(i = 0; i &lt; ary1.Length; i++)<BR> {<BR> for(j = 0; j &lt; ary2.Length; j++)<BR> {<BR> if(ary1[i] == ary2[j])<BR> {<BR> foundDupe = true;<BR> break;<BR> }<BR> }<BR> if(foundDupe)<BR> break;<BR> }<BR> return foundDupe;<BR> }<BR>[/code]

6. Junior Member
Join Date
Dec 1969
Posts
11

## RE: Grrrrr.....

Thanks, I will try that!

7. Senior Member
Join Date
Dec 1969
Posts
2,243

## RE: Grrrrr.....

No Problem. Good Luck.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•