9 Replies - 5907 Views - Last Post: 14 November 2012 - 07:10 AM Rate Topic: -----

#1 mm8273  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 12-November 12

C# - Count the elements in 2 arrays that are in the same position

Posted 12 November 2012 - 09:18 AM

Hi everyone. First off, I just wanted to say that I am working on homework. If that means you can't help me, no problem. I just wanted to see if what I am trying to do is possibler I am trying to determine how many elements from 2 arrays are the same, and then how many are in the same position.

The code I have to determine how many are the same:
char[] charArray1 = playerOne.ToCharArray();
char[] charArray2 = playerTwo.ToCharArray();

bool hasCommonElements = charArray1.Intersect(charArray2).Count() > 0;
char[] commonElements = charArray1.Intersect(charArray2).ToArray();

Console.WriteLine("You have " + commonElements.Length + " number(s) right.");


This does exactly, what I need it to, because I don't really care about which elements are the same, just how many there are. I figure this method would work for determining how many are in the correct position as well, but I can't find any information about making it so the order matters. If anyone has any advice, I'd really appreciate it. Thank you.

Is This A Good Question/Topic? 0
  • +

Replies To: C# - Count the elements in 2 arrays that are in the same position

#2 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6528
  • View blog
  • Posts: 14,439
  • Joined: 02-June 10

Re: C# - Count the elements in 2 arrays that are in the same position

Posted 12 November 2012 - 10:00 AM

A loop would be easiest

pseudo code
for (blah blah)
{
   if (array1[index] == array2[index]) add 1 to the counter
}

Was This Post Helpful? 3
  • +
  • -

#3 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 233
  • View blog
  • Posts: 770
  • Joined: 19-October 11

Re: C# - Count the elements in 2 arrays that are in the same position

Posted 12 November 2012 - 10:29 AM

Also Intersect method returns distinct elements from both sources. Try to assign "Abrakadabra" to playerOne, and "Kvarkadabra" to playerTwo for example.
Was This Post Helpful? 1
  • +
  • -

#4 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1020
  • View blog
  • Posts: 2,463
  • Joined: 04-October 09

Re: C# - Count the elements in 2 arrays that are in the same position

Posted 12 November 2012 - 10:38 AM

View Postmm8273, on 12 November 2012 - 09:18 AM, said:

bool hasCommonElements = charArray1.Intersect(charArray2).Count() > 0;
char[] commonElements = charArray1.Intersect(charArray2).ToArray();


The way you have this written, you have to do the Intersect twice. If you do line 2 first you only need to call Intersect once:
char[] commonElements = charArray1.Intersect(charArray2).ToArray();
bool hasCommonElements = commonElements.Length > 0;


Just something that bothers me, calling a method to do the exact same work more than once.
Was This Post Helpful? 2
  • +
  • -

#5 mm8273  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 12-November 12

Re: C# - Count the elements in 2 arrays that are in the same position

Posted 13 November 2012 - 06:49 PM

View PosttlhIn`toq, on 12 November 2012 - 10:00 AM, said:

A loop would be easiest

pseudo code
for (blah blah)
{
   if (array1[index] == array2[index]) add 1 to the counter
}


Thank you :)
Was This Post Helpful? 0
  • +
  • -

#6 Anthonidas  Icon User is offline

  • D.I.C Head

Reputation: 32
  • View blog
  • Posts: 240
  • Joined: 25-April 11

Re: C# - Count the elements in 2 arrays that are in the same position

Posted 14 November 2012 - 01:51 AM

if you have to find out if the value of array1 is contained in the array2 even if not in the same position you could start a loop in the loop ;)

what i mean is something like that:

for(int i = 0; i<=array1.length; i++)
{
    for(int j = 0; j<=array2.length; j++)
    {
        if(array2[j] == array1[i])
            // do what you want...
    }
}


Was This Post Helpful? 1
  • +
  • -

#7 mm8273  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 12-November 12

Re: C# - Count the elements in 2 arrays that are in the same position

Posted 14 November 2012 - 06:00 AM

View PostAnthonidas, on 14 November 2012 - 01:51 AM, said:

if you have to find out if the value of array1 is contained in the array2 even if not in the same position you could start a loop in the loop ;)/>

what i mean is something like that:

for(int i = 0; i<=array1.length; i++)
{
    for(int j = 0; j<=array2.length; j++)
    {
        if(array2[j] == array1[i])
            // do what you want...
    }
}



Thanks! I really appreciate it.

I realized what I had to do (for determining how many are in the correct position) is declare 2 variables in the for loop. This is what I ended up with:

int i;
int c;

for (i = 0, c = 0; i < charArray1.Length; i++)
{
    if (charArray1[i] == charArray2[i])
    {
        c++;
    }
}

Console.WriteLine("You have " + c + " number(s) in the right spot.");


Seems simple now, but I was having a lot of trouble with it haha.
Was This Post Helpful? 0
  • +
  • -

#8 Anthonidas  Icon User is offline

  • D.I.C Head

Reputation: 32
  • View blog
  • Posts: 240
  • Joined: 25-April 11

Re: C# - Count the elements in 2 arrays that are in the same position

Posted 14 November 2012 - 06:18 AM

oh... ok did not understand, that you wanted to know how many are on the right position. I thought you wanted to know how many in both arrays are the same... sory, my bad. but i'm pleased that at least i could point you in the right direction :)
Was This Post Helpful? 0
  • +
  • -

#9 mm8273  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 12-November 12

Re: C# - Count the elements in 2 arrays that are in the same position

Posted 14 November 2012 - 06:47 AM

View PostAnthonidas, on 14 November 2012 - 06:18 AM, said:

oh... ok did not understand, that you wanted to know how many are on the right position. I thought you wanted to know how many in both arrays are the same... sory, my bad. but i'm pleased that at least i could point you in the right direction :)/>


No you were right! I needed to do both :)
Was This Post Helpful? 0
  • +
  • -

#10 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1020
  • View blog
  • Posts: 2,463
  • Joined: 04-October 09

Re: C# - Count the elements in 2 arrays that are in the same position

Posted 14 November 2012 - 07:10 AM

Just a style issue but it's odd to initialize the counter as part of the loop control. Normally you'd do it outside the loop statement:
int count = 0;
for (int i = 0; i < charArray1.Length; i++) { ...

Also odd to declare the loop control variable outside the loop :)

This post has been edited by Momerath: 14 November 2012 - 07:11 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1