0

First of all sorry for my mistakes in English its not my primary language
i have a problem , i have a array like following

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" 
        };

now i just want to know that how many times Pakistan comes with 1 ,
how many times with 2 , 3 , 4 and i need to know this about all India , USA , Iran , UK

Thanks in advance , you guys are my last hope .

3
  • Please stop changing your question. Users are getting frustrated when they are writing answers for question that do exist any more! Commented Jul 14, 2012 at 9:46
  • I am sorry for that one , i thought i might help you guys to answer it .... Sorry to all of you Commented Jul 14, 2012 at 9:47
  • I have provided a new answer. Please have a look. Commented Jul 14, 2012 at 10:00

3 Answers 3

1

This linq will convert the array into a Dictionary>, where the outer dictionary contains the countries names, and inner dictionaries will contain the ocurrence number (the number after ':') and the count for each ocurrence.

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"
                            };

var count = arr1
    .SelectMany(s => s.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
    .GroupBy(s => s.Split(':')[0], s => s.Split(':')[1])
    .ToDictionary(g => g.Key,
         g =>
         {
              var items = g.Distinct();
              var result = new Dictionary<String, int>();
              foreach (var item in items)
                  result[item] = g.Count(gitem => gitem == item);
              return result;
         });

// print the result
foreach(var country in count.Keys)
{
     foreach(var ocurrence in count[country].Keys)
     {
          Console.WriteLine("{0} : {1} = {2}", country, ocurrence, count[country][ocurrence]);
     }
}
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Try the following:

(The following code is now compiled and tested.)

Use a simple data structure to facilitate the task of holding the result of your operation.

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", }

Method to provide the overall result:

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 < 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 <= 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;
}

Method to provide a local result:

// 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 < arr1.Length; i++) {

            subArray = arr1[i].Split(',');

            string current;

            for (int j = 0; j < subArray.Length; j++) {

                current = subArray[j];
                if (
                    current.Equals(country + ":" + number) &&
                    current.Substring(current.Length - 1, 1).Equals(number + "")
                 ) 
                    result++;
            }
        }

        return result;
    }

The following should enable you to run the algorithm

    // 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();
    }

9 Comments

I have updated my answer to meet your new question. Please have a look.
where is numbersToInvestigate is defined ?
It was just a typing error. I've meant the variable numbersToLookFor. I've corrected that.
Can you make it compile-able , it would be really very easey for me
Now it should be correct. I have currently no compiler at hand but I gave you a complete implementation of the algorithm in question that should be compilable.
|
0

The data structure you are using is not rich enough to provide you with that information. Hence you need to parse your string and create a new data structure to be able to provide (sring[][]):

        string[] arr1 = new string[] { 
        "Pakistan,India,USA,Iran,UK",
        "Pakistan,India,USA,Iran,UK", 
        "India,USA,Iran,UK,Pakistan" 
            };

        string[][] richerArray = arr1.Select(x=> x.Split('\'')).ToArray();
        var countPakistanIsFirst = richerArray.Select(x=>x[0] == "Pakistan").Count();

UPDATE

You seem to have changed your question. The answer applies to the original question.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.