So i am new to c sharp and been playing around with my code but could not figure it out. Let say i have the following:
Code : 2 1 1 1 1 2
Guess: 2 2 1 2 2 1
Firstly, ignore the spots where the guess matches the code. After that, only
2 2 2 1
will be left for the guess and
1 1 1 2
for the code
If you see for the rest of the code, there is three 2's for the guess but only one in the code. I want to count the three 2's as only one occurrence but the part of my code accounts for all 3 which i cant seem to get it working to count as one.
Here is my code. I only post the part im having trouble on.
if (guess.Contains(Code[i]))
{
if (guess.Distinct().Count() > 1)
diffSpot++;
}
I tried using distinct to get only unique numbers only and count to see that if its greater than 1 , it means there is dupe (same occurence in a row) but i guess im missing something. Thanks for any helpful hints and suggestions.
Liang,
Assuming that what you're after is this:
I kind of like LINQ for this:
using System.Linq;
int[] Code = {2, 1, 1, 1, 1, 2};
int[] Guess = {2, 2, 1, 2, 2, 1};
int total =
Guess.Select((g, i) => new {g, i})
.Where(x => x.g != Code[x.i])
.Select(x => x.g).Distinct().Count();
Returns 2. Hope this helps.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.