9 Replies - 9158 Views - Last Post: 11 May 2011 - 09:03 AM Rate Topic: -----

#1 TheBadger  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • 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
Was This Post Helpful? 1
  • +
  • -

#3 Ionut  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 385
  • View blog
  • 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

Was This Post Helpful? 1
  • +
  • -

#4 Vator  Icon User is offline

  • New D.I.C Head

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

Re: Compare two Arrays

Posted 10 May 2011 - 12:57 PM

View PostTheBadger, 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

Was This Post Helpful? 0
  • +
  • -

#5 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4449
  • View blog
  • Posts: 7,744
  • 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.
Was This Post Helpful? 1
  • +
  • -

#6 Ionut  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 385
  • View blog
  • Posts: 1,057
  • Joined: 17-July 10

Re: Compare two Arrays

Posted 10 May 2011 - 01:16 PM

View PostVator, on 10 May 2011 - 12:57 PM, said:

View PostTheBadger, 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

Was This Post Helpful? 0
  • +
  • -

#7 souptoy  Icon User is offline

  • D.I.C Head


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

Re: Compare two Arrays

Posted 10 May 2011 - 01:33 PM

View PostIonut, 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
Was This Post Helpful? 1
  • +
  • -

#8 RetardedGenius  Icon User is offline

  • >>──(Knee)──►
  • member icon

Reputation: 126
  • View blog
  • 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#. :smartass:

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

View Postsouptoy, on 10 May 2011 - 08:33 PM, said:

View PostIonut, 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

Was This Post Helpful? 0
  • +
  • -

#9 Vator  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#10 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4449
  • View blog
  • Posts: 7,744
  • Joined: 08-June 10

Re: Compare two Arrays

Posted 11 May 2011 - 09:03 AM

View PostRetardedGenius, 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#. :smartass:

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.

View PostVator, 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.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1