0

I am sorry for confusing title, how do i make an array of these elements :

string [] anArray = new string[50];
anArray[0] = "New York";
anArray[1] = "London";
anArray[2] = "New York";
anArray[3] = "London";
anArray[4] = "New York";
anArray[5] = "Chicago";

Fall into an array like this :

anArray[0] = "New York";
anArray[1] = "New York";
anArray[2] = "New York";
anArray[3] = "London";
anArray[4] = "London";
anArray[5] = "Chicago";

Where elements are sorted by amount of equal elements are there in the array. And if for example anArray has an equal amount of elements like :

anArray[0] = "New York";
anArray[1] = "New York";
anArray[2] = "London";
anArray[3] = "London";

How do i make so that a program finds both of these elements and outputs something like: New York, amount of elements 2 London, amount of elements 2

I'll hope you understand, sorry if it may be confusing, thanks for any help here.

1
  • Well yeah i have tried to solve it in my program but havent got anything up, could you elaborate more on Dictionary ? Commented Sep 22, 2016 at 12:27

3 Answers 3

1

Just use Linq GroupBy

        string[] anArray = new string[50];
        anArray[0] = "New York";
        anArray[1] = "London";
        anArray[2] = "New York";
        anArray[3] = "London";
        anArray[4] = "New York";
        anArray[5] = "Chicago";

        var GroupedArray = anArray.GroupBy(a => a);
        foreach (var item in GroupedArray)
        {
            Console.WriteLine(item.Key + " -> " + item.Count());
        }
        Console.Read();

        //NewYork -> 3
        //London -> 2
        //Chicago -> 1

On a side note, you initialized the array to hold 50 items which in my example will result in 44 empty string items being taken into account by the groupby clause. If you want to avoid displaying these, replace the groupby line for this one:

var GroupedArray = anArray.GroupBy(a => a).Where(a => !string.IsNullOrEmpty(a.Key));
Sign up to request clarification or add additional context in comments.

1 Comment

to avoid it i just added and if statement : if (item.Key != null) Console.WriteLine(item.Key + " -> " + item.Count());
0

Maybe not the fastest solution, but this is the logic you are after.

var result = anArray.OrderByDescending(o => anArray.Count(c => c == o));

note that you are using an array of size 50, so the first 44 items are going to be null, due to the orderby.

Comments

0

You can order by the count of each element

var orderedItems = anArray.OrderByDescending(x => x?.Count()).ToArray();

Or you could do it by the length of each string

var orderedItems = anArray.OrderByDescending(x => x?.Length).ToArray();

Both check and return null if the array element is null and returns an ordered array.

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.