# Finding the number of unique solutions in a .txt file

Well, my question is the following: I have a .txt file that consists of the following data:

r&=3&a&=3&b&=4&c&=10\\
r&=3&a&=8&b&=15&c&=120\\
r&=3&a&=20&b&=55&c&=1540\\
r&=4&a&=34&b&=119&c&=7140\\
r&=5&a&=24&b&=70&c&=4900\\
r&=5&a&=11&b&=22&c&=946\\
r&=9&a&=10&b&=19&c&=1045\\
r&=10&a&=18&b&=45&c&=5985\\
r&=10&a&=49785&b&=6413415&c&=123395663059845\\
r&=13&a&=91839&b&=16068720&c&=774611255177760\\
...
...
...


I want to use Mathematica to find the total number of unique solutions in the dataset for the value of r. So in my example from above I see the following:

• The number $$r=3$$ appears 3 times;
• The number $$r=4$$ appears 1 time;
• The number $$r=5$$ appears 2 times;
• The number $$r=9$$ appears 1 time;
• The number $$r=10$$ appears 2 times;
• The number $$r=13$$ appears 1 time;

So, the number that Mathematica must find is $$6$$ because in the dataset are $$6$$ different/unique values for r.

In order to input my file I can use:

In[1]:=Import["name of the file.txt", "Data"]

Out[1]={{r&=3&a&=3&b&=4&c&=10\\},{r&=3&a&=8&b&=15&c&=120\\},
{r&=3&a&=20&b&=55&c&=1540\\},{r&=4&a&=34&b&=119&c&=7140\\},
{r&=5&a&=24&b&=70&c&=4900\\},{r&=5&a&=11&b&=22&c&=946\\},
{r&=9&a&=10&b&=19&c&=1045\\},{r&=10&a&=18&b&=45&c&=5985\\},
{r&=10&a&=49785&b&=6413415&c&=123395663059845\\},
{r&=13&a&=91839&b&=16068720&c&=774611255177760\\}}


But how to write code to find the number I am looking for, I do not know. Note: the data I have is way bigger then the small dataset I presented (as example) above.

Not sure whether I got your right, but maybe this is ist: You can use StringCases in combination with Counts to get the corresponding results. Given your input as data (I don't have your textfile):

data = "r&=3&a&=3&b&=4&c&=10\\
r&=3&a&=8&b&=15&c&=120\\
r&=3&a&=20&b&=55&c&=1540\\
r&=4&a&=34&b&=119&c&=7140\\
r&=5&a&=24&b&=70&c&=4900\\
r&=5&a&=11&b&=22&c&=946\\
r&=9&a&=10&b&=19&c&=1045\\
r&=10&a&=18&b&=45&c&=5985\\
r&=10&a&=49785&b&=6413415&c&=123395663059845\\
r&=13&a&=91839&b&=16068720&c&=774611255177760\\"


then

 Counts @ StringCases[data, "r&=" ~~ x : DigitCharacter .. -> x]


delivers an association with the multiplicities:

<|"3" -> 3, "4" -> 1, "5" -> 2, "9" -> 1, "10" -> 2, "13" -> 1|>

• First of all, thank you for your answer. I edited my question, so if you want to take a look at it. Because: you copy and paste the data from my file, but in reality, my data is very big so it is not convenient to paste it directly into Mathematica. Besides that I want Mathematica to output the unique number of solutions that is given in the data for $r$, so in the data you used it must give the number $6$. Can you help me with that? Thanks a lot. Dec 28, 2020 at 12:38
• Do you understand what I am asking, or must I explain myself a bit more? Thanks in advance. Dec 28, 2020 at 14:05
• Try this_: StringCases[data, "r&=" ~~ x : NumberString -> x] // Union // Length Dec 28, 2020 at 14:44
• @Jan If your file is to large you can apply the function to every line via ´out = Counts /@ (StringCases[#, "r&=" ~~ x : DigitCharacter .. -> x] & /@ {your lines here})´ then ´Plus @@ out´ Dec 28, 2020 at 14:48
• @DanielHuber If I want to use that I can not use data = Import["name of the file.txt", "Data"]; as the data file. How can I fix that? Dec 28, 2020 at 14:52

The code that I was looking for is:

data = Import["name of the file.txt", "Data"];
StringCases[data, "r&=" ~~ x : NumberString -> x] // Union // Length