找到java数组中元素的频率 [英] find the frequency of elements in a java array

查看:300
本文介绍了找到java数组中元素的频率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个int数组:

{1,2,4,2,3,5,6,4,3}

如何找到 1 = 1等数组元素的频率, 2 = 2,3 = 2,4 = 4 .. 。我需要一个类,我可以传递我的数组并返回一个数组,给出数组元素的数量。例如: - 数组{[0] = 1,[1] = 2,[2] = 3,[3] = 4 ..} (对于上面的示例数组) ;

How can I find frequencies of array elements like 1=1,2=2,3=2,4=4... I need a class which I can pass my array to and return an array which gives the count of array elements. ex:- array{[0]=1,[1]=2,[2]=3,[3]=4..} (for above example array);

推荐答案

你必须做一些事情:


  1. 定义数字范围的上限和下限。

  2. 建立一个方便的对象/数据结构来存储这些数字的出现次数。

  3. 遍历传入的数组并计算每个数字的所有匹配项,将结果存储在方便的对象/数据结构中。

如果这是以一种简单的方式完成的,那么只需从传入的数组中读取元素并打印出最终结果即可。

If this is done in a simple manner, it could be only a matter of reading the elements from the passed-in array and printing out the final result.

这篇关于找到java数组中元素的频率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆