我有一个整型数组:
{1,2,4,2,3,5,6,4,3}
我该如何找到数组元素的频率,例如
1=1,2=2,3=2,4=4..
。我需要一个类,可以将我的数组传递给它,并返回一个数组,其中包含数组元素的计数。例如: array{[0]=1,[1]=2,[2]=3,[3]=4..}
(对于上述示例数组)。我有一个整型数组:
{1,2,4,2,3,5,6,4,3}
1=1,2=2,3=2,4=4..
。我需要一个类,可以将我的数组传递给它,并返回一个数组,其中包含数组元素的计数。例如: array{[0]=1,[1]=2,[2]=3,[3]=4..}
(对于上述示例数组)。class MapTest
{
public static void main(String args[]){
HashMap<Integer,Integer> h = new HashMap<Integer,Integer>();
int arr[] = new int[]{2,2,3,3,5,6,7,9,9,0};
for(int i=0; i<arr.length; i++){
if(h.containsKey(arr[i])){
h.put(arr[i], h.get(arr[i]) + 1);
} else {
h.put(arr[i], 1);
}
}
System.out.println(h);
}
}
Map<Integer, Long> freq = Arrays.stream(array).boxed().
collect(Collectors.groupingBy(Integer::intValue, Collectors.counting()));
Function.identity()
而不是Integer::intValue
是否足够?在这种情况下,我们实际上不需要取消封箱整数。 - Anton BalaniucMap<Integer, Long> freq = Arrays.stream(a).boxed().
collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
public Object[] getFrequencies(int[] a) {
Map<Integer, Long> freq = Arrays.stream(a).boxed().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
return freq.entrySet().toArray();
}
你需要做以下几件事情:
如果以简单的方式完成,只需从传入的数组中读取元素并打印输出最终结果即可。
import java.util.*;
class Findfreqarray
{
public static void main(String args[])
{
int t, i, j, len, count=0;
Scanner in = new Scanner(System.in);
System.out.println("Enter number of elements to insert in an array: ");
len = in.nextInt();
int[] arr = new int[len];
System.out.println("Enter elements to insert in an array: ");
for(i=0;i<len;i++)
{
t = in.nextInt();
arr[i] = t;
}
System.out.println("\n");
for(i=0;i<len;i++)
{
count=1;
for(j=i+1;j<=len-1;j++)
{
if(arr[i]==arr[j] && arr[i]!='\0')
{
count++;
arr[j] = '\0';
}
}
if(arr[i]!='\0')
{
System.out.println(arr[i] + " is " + count + " times.\n");
}
}
}
}
public static void findCount3(int[] a){
Map<Integer, Integer> hm = new HashMap<Integer, Integer>();
for(int i = 0; i < a.length; i++){
if(!hm.containsKey(a[i])){
hm.put(a[i], 1);
}else{
hm.put(a[i], hm.get(a[i])+1);
}
System.out.println(hm);
}
我有一个解决方案,可以计算Java数组中元素的频率。
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class ItemCount {
public static void main(String[] args)
{
try{
int count=1,index=1;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the Size of array : ");
int size=Integer.parseInt(br.readLine());
System.out.print("Enter the Elements of array : ");
int arr[]=new int[size];
for(int i=0;i<arr.length;i++)
{
System.out.print("arr["+i+"] : ");
arr[i]=Integer.parseInt(br.readLine());
}
System.out.print("Sorted Array is :");
SortingArray.sortDescendind(arr);
for(int i=0;i<arr.length;i++)
{
System.out.println("arr["+i+"] : "+arr[i]);
}
for(int i=0;i<arr.length;)
{
count=1;
for(index=i+1;index<arr.length;index++)
{
if(arr[i]==arr[index])
{
count++;
}
else{
break;
}
}
System.out.println(""+arr[i] +"----> "+count);
i+=count;
}
}catch(Exception ex)
{
ex.printStackTrace();
}
}
}
/// 你可以选择任何排序方法来对数组进行排序——>SortingArray.sortDescendind(arr)
{element, occurrences}
添加到计数数组中,如果遇到旧元素,请在其出现位置上加1。 - Lightfire228不在此透露,这是一个很好的起点:
int[] array = {1,2,4,2,3,5,6,4,3};
public int[] (array){
//need to perform a sort...or a search
//after searching check for matches,
//sorting could make performing comparisons more efficient
//not all searches/sorts are created equal.
int[array.length] result += {"["+numberChecked+"]="+freqOccurred};
return result;
}
这段代码尚未编译,因此请将其视为伪代码。其目的是让您思考如何实现所需的目标。可能已经存在一个Java包,可以检查数组中的频率元素,但这很可能是您正在寻找的内容。祝你好运。
Map<Integer,Integer> h = new HashMap<>();
int arr[] = new int[]{2,2,3,3,5,6,7,9,9,0};
for(int elem:arr) {
h.merge(elem, 1, Integer::sum);
}
Hashmap.merge()允许您指定如何更新哈希映射中的值。如果elem不存在,则类似于h.put(elem,1)。如果存在名为oldFreq的现有值,则会用Integer.sum(oldFreq, 1)替换它。
package practice.learning;
import java.util.Arrays;
public class FreqElem {
public static void main(String[] args) {
// TODO Auto-generated method stub
String[] names= {"Pawan","Rohan","Sudesh","Pawan","Shiv"};
for(int i=0;i<names.length;i++) {
int count=0;
//System.out.print(names[i]);
String a=names[i];
for(int j=0;j<names.length;j++) {
if(names[j]==a) {
count+=1;
}
}
System.out.println(names[i]+":"+count);
}
}
}
Collections.frequency(Arrays.asList(...), X)
- obatakuArrays.asList(int[])
方法会把整个int[]
数组作为一个对象处理,并返回一个只包含该数组的单例列表List<int[]>
。如果想要得到每个元素的封装类型Integer[]
,可以直接使用可变参数或传递Integer[]
数组。具体实现可以参考这个链接:http://ideone.com/TwS3yJ - obataku