c# - How do you find the longest array in a dictionary without referencing a specific key -


i have dictionary:

public static void main(string[] args) {     dictionary<string, int[]> ret = new dictionary<string, int[]>();     int[] = {1,0,3,4,0};     int[] b = { 3, 0, 9, 10, 0};     int[] c = {2,3,3,5,0};     ret.add("jack", a);     ret.add("jane", b);     ret.add("james", c);      int total = ret["jack"].length;      console.writeline(total); } 

to find longest length use:

int total = ret["jack"].length; 

is there way find longest length without referencing specific key?

you have iterate through every key/value pair in dictionary, check length of value, , remember largest.

luckily, there's linq extension called max you:

int total = ret.max(t => t.value.length); 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -