Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would use the String.Split(char[]) method and the String.SubString(int, int) method to inspect every 'country' inside your array and to get the number postfix of each country.</p> <p>Try the following:</p> <p><em>(The following code is now compiled and tested.)</em></p> <p>Use a simple data structure to facilitate the task of holding the result of your operation.</p> <pre><code>public struct Result { string Country { get; set; } int Number { get; set; } int Occurrences { get; set; } } // define what countries you are dealing with string[] countries = new string[] { "Pakistan", "India", "USA", "Iran", "UK", } </code></pre> <p>Method to provide the overall result:</p> <pre><code>public static Result[] IterateOverAllCountries () { // range of numbers forming the postfix of your country strings int numbersToLookFor = 4; // provide an array that stores all the local results // numbersToLookFor + 1 to respect that numbers are starting with 0 Result[] result = new Result[countries.Length * (numbersToLookFor + 1)]; string currentCountry; int c = 0; // iterate over all countries for (int i = 0; i &lt; countries.Length; i++) { currentCountry = countries[i]; int j = 0; // do that for every number beginning with 0 // (according to your question) int localResult; while (j &lt;= numbersToLookFor) { localResult = FindCountryPosition(currentCountry, j); // add another result to the array of all results result[c] = new Result() { Country = currentCountry, Number = j, Occurrences = localResult }; j++; c++; } } return result; } </code></pre> <p>Method to provide a local result:</p> <pre><code>// iterate over the whole array and search the // occurrences of one particular country with one postfix number public static int FindCountryPosition (string country, int number) { int result = 0; string[] subArray; for (int i = 0; i &lt; arr1.Length; i++) { subArray = arr1[i].Split(','); string current; for (int j = 0; j &lt; subArray.Length; j++) { current = subArray[j]; if ( current.Equals(country + ":" + number) &amp;&amp; current.Substring(current.Length - 1, 1).Equals(number + "") ) result++; } } return result; } </code></pre> <p>The following should enable you to run the algorithm</p> <pre><code> // define what countries you are dealing with static string[] countries = new string[] { "Pakistan", "India", "USA", "Iran", "UK", }; static string[] arr1 = new string[] { "Pakistan:4,India:3,USA:2,Iran:1,UK:0", "Pakistan:4,India:3,USA:2,Iran:1,UK:0", "India:4,USA:3,Iran:2,UK:1,Pakistan:0" }; static void Main (string[] args) { Result[] r = IterateOverAllCountries(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload