Compare two Arrays

Page 1 of 1

9 Replies - 20090 Views - Last Post: 11 May 2011 - 09:03 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=231454&amp;s=dcb54ce6a9c3ec13d44d1bfbca12a763&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

Reputation: 0
• Posts: 7
• Joined: 29-April 11

Compare two Arrays

Posted 10 May 2011 - 11:40 AM

Compare the two and just return how many match, don't need to know which ones matched

Having a bit of brain freeze on this right now, any got any ideas?
Is This A Good Question/Topic? 0

Replies To: Compare two Arrays

#2 smohd

• Critical Section

Reputation: 1822
• Posts: 4,627
• Joined: 14-March 10

Re: Compare two Arrays

Posted 10 May 2011 - 11:47 AM

You need to loop in the array. Use loop to check the comparison of your array lists.
You may have a count that count if any of indices has same values

#3 Ionut

• D.I.C Lover

Reputation: 386
• Posts: 1,057
• Joined: 17-July 10

Re: Compare two Arrays

Posted 10 May 2011 - 11:53 AM

1. classic O(n^2) solution with two for loops
2. using LINQ
3. using lambda expression(what you need is .Count and .Contains methods of the array objects)

Choose the one you are more confortable with.

This post has been edited by Ionut: 10 May 2011 - 11:53 AM

#4 Vator

Reputation: 1
• Posts: 5
• Joined: 10-May 11

Re: Compare two Arrays

Posted 10 May 2011 - 12:57 PM

TheBadger, on 10 May 2011 - 11:40 AM, said:

Compare the two and just return how many match, don't need to know which ones matched

Having a bit of brain freeze on this right now, any got any ideas?

That's an example:

```  string[] array1 = { "C#", "C++", "Visual Basic" };
string[] array2 = { "C#", "C++", "Visual Basic" };
for(int i = 0;i < array1.Length;i++)
{
string comp1 = array1[i];
string comp2 = array2[i];
if (comp1 == comp2)
{
//Do something
}
}
```

I used a for loop to get every string in both arrays,and then I compared them.
You can use a foreach loop,if you want.

For the count of matching words,just create a int variable,and increment it when "comp1 == comp2".Then,return this value

Hope it helps,sorry for my bad English.

This post has been edited by Vator: 10 May 2011 - 01:00 PM

#5 Curtis Rutland

• （╯°□°）╯︵ (~ .o.)~

Reputation: 5103
• Posts: 9,283
• Joined: 08-June 10

Re: Compare two Arrays

Posted 10 May 2011 - 01:11 PM

@Vator, there's a reason why the two smart people before you didn't give a copy/pastable example.

It's because our policy here is not to give away the answers to homework questions, but guide the asker to learning it themselves. Don't just dump code, even with a good explanation, on questions like this.

#6 Ionut

• D.I.C Lover

Reputation: 386
• Posts: 1,057
• Joined: 17-July 10

Re: Compare two Arrays

Posted 10 May 2011 - 01:16 PM

Vator, on 10 May 2011 - 12:57 PM, said:

TheBadger, on 10 May 2011 - 11:40 AM, said:

Compare the two and just return how many match, don't need to know which ones matched

Having a bit of brain freeze on this right now, any got any ideas?

That's an example:

```  string[] array1 = { "C#", "C++", "Visual Basic" };
string[] array2 = { "C#", "C++", "Visual Basic" };
for(int i = 0;i < array1.Length;i++)
{
string comp1 = array1[i];
string comp2 = array2[i];

if (comp1 == comp2)
{
//Do something
}
}
```

I used a for loop to get every string in both arrays,and then I compared them.
You can use a foreach loop,if you want.

For the count of matching words,just create a int variable,and increment it when "comp1 == comp2".Then,return this value

Hope it helps,sorry for my bad English.

This is not the best solution. For example:
``` string[] array1 = { "C++", "C#", "Visual Basic" };
string[] array2 = { "C#", "Visual Basic", "C++" };

```

In this case, this algorithm you wrote would give a solution equal to 0, even if there are 3 matches.

This post has been edited by Ionut: 10 May 2011 - 01:18 PM

#7 souptoy

Reputation: 54
• Posts: 244
• Joined: 17-January 08

Re: Compare two Arrays

Posted 10 May 2011 - 01:33 PM

Ionut, on 10 May 2011 - 12:53 PM, said:

1. classic O(n^2) solution with two for loops

Reminded me of macosxnerd101's blog post: Why Math Is Important For Programmers

#8 RetardedGenius

• ﻿>>──(Knee)──►

Reputation: 127
• Posts: 555
• Joined: 30-October 10

Re: Compare two Arrays

Posted 10 May 2011 - 01:48 PM

What you want to do is iterate through both of the arrays simultaneously, and compare the values in both arrays of the same index sequentially, every time you find a match simply increment a counter variable (which starts at 0.)

It is possible to do all of this in a single line of code of C#.

Note: this will result in O(n) comparisons, not O(n²) as previously stated.

souptoy, on 10 May 2011 - 08:33 PM, said:

Ionut, on 10 May 2011 - 12:53 PM, said:

1. classic O(n^2) solution with two for loops

Reminded me of macosxnerd101's blog post: Why Math Is Important For Programmers

I commented on that as well, however if you read the OP's question, it is somewhat ambiguous as to whether he wants to count the number of array items of the same index which match, or the number of items in present in array1 which are also present in array2. I inferred the first option, hence the answer I gave...

This post has been edited by RetardedGenius: 10 May 2011 - 01:49 PM

#9 Vator

Reputation: 1
• Posts: 5
• Joined: 10-May 11

Re: Compare two Arrays

Posted 11 May 2011 - 08:55 AM

@Curtis Rutland

I'm sorry.I am new here.

I didn't do his homework.I gave him a very simple example of doing that for compare 100% equal arrays.
For what he is looking for,he must think.It's simple,in my opinion.

#10 Curtis Rutland

• （╯°□°）╯︵ (~ .o.)~

Reputation: 5103
• Posts: 9,283
• Joined: 08-June 10

Re: Compare two Arrays

Posted 11 May 2011 - 09:03 AM

RetardedGenius, on 10 May 2011 - 03:48 PM, said:

What you want to do is iterate through both of the arrays simultaneously, and compare the values in both arrays of the same index sequentially, every time you find a match simply increment a counter variable (which starts at 0.)

It is possible to do all of this in a single line of code of C#.

Note: this will result in O(n) comparisons, not O(nē) as previously stated.

That assumes that the requirement isn't to find any matches, just matches of the same index. If the requirement is to find any matches, you'd have to sort the arrays first, which would increase the time by whatever sorting algorithm you use.

Vator, on 11 May 2011 - 10:55 AM, said:

@Curtis Rutland

I'm sorry.I am new here.

I didn't do his homework.I gave him a very simple example of doing that for compare 100% equal arrays.
For what he is looking for,he must think.It's simple,in my opinion.

I understand you're new, that's why I'm explaining the rules to you. Don't provide copy/pastable code on homework questions. Even if it doesn't do 100% of the work, it's still counterproductive, since our goal isn't to help these people pass a class, it's to help them become better programmers. Make them work for each step. Just trust me on this, I've been here a while.