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.

Hi @RobotWarrior

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

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
            MatchingGuess = MatchingGuess + 1

Matching guess will give you how many numbers got matched…

Hope it helps…


Thank you so much. This has been very helpful.

1 Like

Hi @RobotWarrior

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 :slight_smile:

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 :slight_smile:

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.