面试题:统计数组中元素出现的次数

面试题描述:

统计一个数组中,重复字符出现的次数,并以关联数组的形式输出。 

        String[] arr = {"D","B","A","B","C","A","A","B","D","C","D","D"};
		
		Map<String, Long> r = 
				Stream.of(arr).collect(
						Collectors.groupingBy(Function.identity(), Collectors.counting())
						);
		
		System.out.println(r);
tangcheng
代码片段
tangcheng
控制台结果

如果需要找出出现次数最多的字符串,可以使用如下代码:

找出次数最多的字符串

猜你喜欢

转载自blog.csdn.net/weixin_42704316/article/details/82947259