# Count number of matches between 4 digit string and another 4 digits string

Hi Folks, I would like to count the number of correctly guessed numbers for 4 digits with another 4 digits. Eg. 7777 and 2772. The result should give two correctly guessed numbers.
Eg. 7037 and 3773. The result should give 3 correctly guessed numbers. Any help will be much appreciated. Thanks.

Welcome to the community.

You can do it like this.
Letâ€™s consider the user input value and the random value as two different variables.
Int randomval = 2772
Int userVal = 2712

Now letâ€™s convert these two into two arrays to break the numbers into separate elements
RandomVal.ToString.ToArray()
UserVal.ToString.ToArray()

Now to make processing easy we can convert them back to Lists
Define a string List
List random list = new List(of String)
List UserList = new List(of String)

Now assign the two arrays to lists
Array name.ToList()

Why convert to list is because you can easily remove the duplicate numbers from each set. This makes it easy to compare. So for example if one input has 2772 it will have 2 and 7 after duplicate removal.

So to remove duplicate values you use below code

List name.Distinct().ToList()

After doing that for both lists, use two nested for each loop activities to run through each list.

``````Int MatchingGuess = 0
For each list1
For each list2
If(List1Item.equals(list2Item)
MatchingGuess = MatchingGuess + 1
``````

Matching guess will give you how many numbers got matchedâ€¦

Hope it helpsâ€¦

5 Likes

Thank you so much. This has been very helpful.

1 Like

Glad to hear that I was able to helpâ€¦ if my answer helped you to get it done, please also mark it as the solution tooâ€¦

Thank you!!
Happy automatingâ€¦

Iâ€™ll just comment with another way as the one mentioned by @Lahiru.Fernando likely works, it just isnâ€™t intuitive to me. This just goes to show that thereâ€™s always more than one solution to the same problem in uipath or any other programming!

I would convert both strings to a character array called arrGuess and arrAnswer, Iterate through each guess in the array with a for each loop and compare the guess to the corresponding index in the answer array. Increment a counter each time a match is found.

In the end itâ€™s largely the same, just a different way to go about it that seems more intuitive to me personally

1 Like

Yeahâ€¦ I do agree. There are different ways of doing the same thing. Someone may feel better with one approach and another with another approach. It happens

1 Like

Thank you for sharing too! Appreciate you inputs too.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.