Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在数组中查找模态数_Java_Arrays_Topmost - Fatal编程技术网

Java 在数组中查找模态数

Java 在数组中查找模态数,java,arrays,topmost,Java,Arrays,Topmost,我有这样的数组: int[] array = { 2, 4, 6, 8, -3, 8, 2, 7, 2, 4 }; Map<Integer, Integer> frequencies = new HashMap<>(); for (int n : array) { frequencies.put(n, frequencies.getOrDefault(n, 0) + 1); // Java 8 } if (!frequencies.isEmpty()) { I

我有这样的数组:

int[] array = { 2, 4, 6, 8, -3, 8, 2, 7, 2, 4 };
Map<Integer, Integer> frequencies = new HashMap<>();
for (int n : array) {
  frequencies.put(n, frequencies.getOrDefault(n, 0) + 1); // Java 8
}

if (!frequencies.isEmpty()) {
  Integer bestMatch = null;
  Integer lastCount = null;
  for (Map.Entry<Integer,Integer> entry : frequencies.entrySet()) {
    Integer count = entry.getValue(); 
    if (null == lastCount || count > lastCount) {
      bestMatch = entry.getKey(); 
      lastCount = count;
    }
  }
  System.out.println("Most frequent value: " + bestMatch + " (found " + lastCount + " times)");
}

我想知道哪一个数字显示的时间最多(在本例中,当然是2),我如何才能做到这一点?

您可以构建一个地图,将整数映射到它们的计数。在Java中,类似于HashMap的东西。浏览列表并检查int是否已经在映射中。如果是,将1添加到其计数中,否则将其初始化为1。然后,返回地图,找到计数最高的数字

您可以构造一个映射int到其计数的映射。在Java中,类似于HashMap的东西。浏览列表并检查int是否已经在映射中。如果是,将1添加到其计数中,否则将其初始化为1。然后,返回地图,找到计数最高的数字

您可以构造一个映射int到其计数的映射。在Java中,类似于HashMap的东西。浏览列表并检查int是否已经在映射中。如果是,将1添加到其计数中,否则将其初始化为1。然后,返回地图,找到计数最高的数字

您可以构造一个映射int到其计数的映射。在Java中,类似于HashMap的东西。浏览列表并检查int是否已经在映射中。如果是,将1添加到其计数中,否则将其初始化为1。然后,返回地图,找到计数最高的数字

您需要创建频率分布并遍历数组。当您使用整数时,这使得它很容易,因为它们可以直接用作键

HashTable frequencyDistribution = new HashTable();
for(int i=0; i<array.length; i++) {
    int key = array[i];
    if( !frequencyDistribution.containsKey( key ) ) frequencyDistribution.add( key, 0 );
    frequencyDistribution[ key ]++;
}
int modeKey;
int modeCnt = int.MIN;
foreach(int key in frequencyDistribution.keys) {
    int cnt = frequencyDistribution[key];
    if( cnt > modeCnt ) modeKey = key;
}
print("Most frequent is: {0} as it appears {1} times.", modeKey, modeCnt);
HashTable frequencyDistribution=new HashTable();
对于(int i=0;i modeCnt)modeKey=key;
}
打印(“最常见的是:{0},因为它出现了{1}次。”,modeKey,modeCnt);

您需要创建频率分布并遍历数组。当您使用整数时,这使得它很容易,因为它们可以直接用作键

HashTable frequencyDistribution = new HashTable();
for(int i=0; i<array.length; i++) {
    int key = array[i];
    if( !frequencyDistribution.containsKey( key ) ) frequencyDistribution.add( key, 0 );
    frequencyDistribution[ key ]++;
}
int modeKey;
int modeCnt = int.MIN;
foreach(int key in frequencyDistribution.keys) {
    int cnt = frequencyDistribution[key];
    if( cnt > modeCnt ) modeKey = key;
}
print("Most frequent is: {0} as it appears {1} times.", modeKey, modeCnt);
HashTable frequencyDistribution=new HashTable();
对于(int i=0;i modeCnt)modeKey=key;
}
打印(“最常见的是:{0},因为它出现了{1}次。”,modeKey,modeCnt);

您需要创建频率分布并遍历数组。当您使用整数时,这使得它很容易,因为它们可以直接用作键

HashTable frequencyDistribution = new HashTable();
for(int i=0; i<array.length; i++) {
    int key = array[i];
    if( !frequencyDistribution.containsKey( key ) ) frequencyDistribution.add( key, 0 );
    frequencyDistribution[ key ]++;
}
int modeKey;
int modeCnt = int.MIN;
foreach(int key in frequencyDistribution.keys) {
    int cnt = frequencyDistribution[key];
    if( cnt > modeCnt ) modeKey = key;
}
print("Most frequent is: {0} as it appears {1} times.", modeKey, modeCnt);
HashTable frequencyDistribution=new HashTable();
对于(int i=0;i modeCnt)modeKey=key;
}
打印(“最常见的是:{0},因为它出现了{1}次。”,modeKey,modeCnt);

您需要创建频率分布并遍历数组。当您使用整数时,这使得它很容易,因为它们可以直接用作键

HashTable frequencyDistribution = new HashTable();
for(int i=0; i<array.length; i++) {
    int key = array[i];
    if( !frequencyDistribution.containsKey( key ) ) frequencyDistribution.add( key, 0 );
    frequencyDistribution[ key ]++;
}
int modeKey;
int modeCnt = int.MIN;
foreach(int key in frequencyDistribution.keys) {
    int cnt = frequencyDistribution[key];
    if( cnt > modeCnt ) modeKey = key;
}
print("Most frequent is: {0} as it appears {1} times.", modeKey, modeCnt);
HashTable frequencyDistribution=new HashTable();
对于(int i=0;i modeCnt)modeKey=key;
}
打印(“最常见的是:{0},因为它出现了{1}次。”,modeKey,modeCnt);

我想下面的代码可以在Java8中实现这一点

int[] array = { 2, 4, 6, 8, -3, 8, 2, 7, 2, 4 };
Arrays.stream(array).collect(Collectors.groupingBy(s -> s))
      .forEach((k, v) -> System.out.println(k + ": " + v.size()));
印刷品:

2: 3
4: 2
8: 2
6: 1
7: 1
-3: 1

我想下面的代码将在Java8中实现这一点

int[] array = { 2, 4, 6, 8, -3, 8, 2, 7, 2, 4 };
Arrays.stream(array).collect(Collectors.groupingBy(s -> s))
      .forEach((k, v) -> System.out.println(k + ": " + v.size()));
印刷品:

2: 3
4: 2
8: 2
6: 1
7: 1
-3: 1

我想下面的代码将在Java8中实现这一点

int[] array = { 2, 4, 6, 8, -3, 8, 2, 7, 2, 4 };
Arrays.stream(array).collect(Collectors.groupingBy(s -> s))
      .forEach((k, v) -> System.out.println(k + ": " + v.size()));
印刷品:

2: 3
4: 2
8: 2
6: 1
7: 1
-3: 1

我想下面的代码将在Java8中实现这一点

int[] array = { 2, 4, 6, 8, -3, 8, 2, 7, 2, 4 };
Arrays.stream(array).collect(Collectors.groupingBy(s -> s))
      .forEach((k, v) -> System.out.println(k + ": " + v.size()));
印刷品:

2: 3
4: 2
8: 2
6: 1
7: 1
-3: 1

您可以使用bucket sort的bucket filling步骤,然后线性扫描所有bucket,以获得出现频率最高的数字(您知道如何获得数组中的最大数字,对吗?)。底层数据结构不必是数组(最快的),您可以使用任何具有键->值机制的东西,因为对于大范围,由于内存限制,可能无法使用数组,以降低运行时间为代价。

您可以使用bucket sort的bucket filling步骤,然后线性扫描所有bucket,以获得出现频率最高的数字(您知道如何获得数组中的最大数字,对吗?)。底层数据结构不必是数组(最快的),您可以使用任何具有键->值机制的东西,因为对于大范围,由于内存限制,可能无法使用数组,以降低运行时间为代价。

您可以使用bucket sort的bucket filling步骤,然后线性扫描所有bucket,以获得出现频率最高的数字(您知道如何获得数组中的最大数字,对吗?)。底层数据结构不必是数组(最快的),您可以使用任何具有键->值机制的东西,因为对于大范围,由于内存限制,可能无法使用数组,以降低运行时间为代价。

您可以使用bucket sort的bucket filling步骤,然后线性扫描所有bucket,以获得出现频率最高的数字(您知道如何获得数组中的最大数字,对吗?)。底层数据结构不必是数组(最快的),您可以使用任何具有键->值机制的东西,因为对于大范围,由于内存限制,数组可能不可能实现,而运行时间会变慢。

您可以执行如下算法:

int[] array = { 2, 4, 6, 8, -3, 8, 2, 7, 2, 4 };
Map<Integer, Integer> frequencies = new HashMap<>();
for (int n : array) {
  frequencies.put(n, frequencies.getOrDefault(n, 0) + 1); // Java 8
}

if (!frequencies.isEmpty()) {
  Integer bestMatch = null;
  Integer lastCount = null;
  for (Map.Entry<Integer,Integer> entry : frequencies.entrySet()) {
    Integer count = entry.getValue(); 
    if (null == lastCount || count > lastCount) {
      bestMatch = entry.getKey(); 
      lastCount = count;
    }
  }
  System.out.println("Most frequent value: " + bestMatch + " (found " + lastCount + " times)");
}
Map frequencies=newhashmap();
for(int n:array){
frequencies.put(n,frequencies.getOrDefault(n,0)+1);//Java8
}
如果(!frequencies.isEmpty()){
整数bestMatch=null;
整数lastCount=null;
对于(Map.Entry:frequencies.entrySet()){
整数计数=entry.getValue();
如果(null==lastCount | | count>lastCount){
bestMatch=entry.getKey();
lastCount=计数;
}
}
System.out.println(“最频繁值:“+bestMatch+”(找到“+lastCount+”次)”);
}
  • 我们首先计算一个值出现的次数
  • 我们在映射中选择最大值并存储相关的键

您可以执行如下算法:

int[] array = { 2, 4, 6, 8, -3, 8, 2, 7, 2, 4 };
Map<Integer, Integer> frequencies = new HashMap<>();
for (int n : array) {
  frequencies.put(n, frequencies.getOrDefault(n, 0) + 1); // Java 8
}

if (!frequencies.isEmpty()) {
  Integer bestMatch = null;
  Integer lastCount = null;
  for (Map.Entry<Integer,Integer> entry : frequencies.entrySet()) {
    Integer count = entry.getValue(); 
    if (null == lastCount || count > lastCount) {
      bestMatch = entry.getKey(); 
      lastCount = count;
    }
  }
  System.out.println("Most frequent value: " + bestMatch + " (found " + lastCount + " times)");
}
Map frequencies=newhashmap();
for(int n:array){
frequencies.put(n,frequencies.getOrDefault(n,0)+1);//Java8
}
如果(!frequencies.isEmpty()){
整数bestMatch=null;
整数lastCount=null;
对于(Map.Entry:frequencies.entrySet()){
整数计数=entry.getValue();
如果(null==lastCount | | count>lastCount){