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
Post a Comment