簡體   English   中英

查找數組中出現次數最多的元素 [java]

[英]Find the element with highest occurrences in an array [java]

我必須在雙數組中找到出現次數最多的元素。 我是這樣做的:

int max = 0;
for (int i = 0; i < array.length; i++) {
       int count = 0;
       for (int j = 0; j < array.length; j++) {
         if (array[i]==array[j])
             count++;
   }
  if (count >= max)
      max = count;
 }

該程序可以運行,但是速度太慢,我必須找到更好的解決方案嗎? 誰能幫我?

我必須在雙數組中查找出現次數最多的元素。 我這樣做是這樣的:

int max = 0;
for (int i = 0; i < array.length; i++) {
       int count = 0;
       for (int j = 0; j < array.length; j++) {
         if (array[i]==array[j])
             count++;
   }
  if (count >= max)
      max = count;
 }

該程序可以運行,但是速度太慢! 我必須找到更好的解決方案,有人可以幫助我嗎?

我必須在雙數組中查找出現次數最多的元素。 我這樣做是這樣的:

int max = 0;
for (int i = 0; i < array.length; i++) {
       int count = 0;
       for (int j = 0; j < array.length; j++) {
         if (array[i]==array[j])
             count++;
   }
  if (count >= max)
      max = count;
 }

該程序可以運行,但是速度太慢! 我必須找到更好的解決方案,有人可以幫助我嗎?

我必須在雙數組中查找出現次數最多的元素。 我這樣做是這樣的:

int max = 0;
for (int i = 0; i < array.length; i++) {
       int count = 0;
       for (int j = 0; j < array.length; j++) {
         if (array[i]==array[j])
             count++;
   }
  if (count >= max)
      max = count;
 }

該程序可以運行,但是速度太慢! 我必須找到更好的解決方案,有人可以幫助我嗎?

我必須在雙數組中查找出現次數最多的元素。 我這樣做是這樣的:

int max = 0;
for (int i = 0; i < array.length; i++) {
       int count = 0;
       for (int j = 0; j < array.length; j++) {
         if (array[i]==array[j])
             count++;
   }
  if (count >= max)
      max = count;
 }

該程序可以運行,但是速度太慢! 我必須找到更好的解決方案,有人可以幫助我嗎?

我必須在雙數組中查找出現次數最多的元素。 我這樣做是這樣的:

int max = 0;
for (int i = 0; i < array.length; i++) {
       int count = 0;
       for (int j = 0; j < array.length; j++) {
         if (array[i]==array[j])
             count++;
   }
  if (count >= max)
      max = count;
 }

該程序可以運行,但是速度太慢! 我必須找到更好的解決方案,有人可以幫助我嗎?

我必須在雙數組中查找出現次數最多的元素。 我這樣做是這樣的:

int max = 0;
for (int i = 0; i < array.length; i++) {
       int count = 0;
       for (int j = 0; j < array.length; j++) {
         if (array[i]==array[j])
             count++;
   }
  if (count >= max)
      max = count;
 }

該程序可以運行,但是速度太慢! 我必須找到更好的解決方案,有人可以幫助我嗎?

我必須在雙數組中查找出現次數最多的元素。 我這樣做是這樣的:

int max = 0;
for (int i = 0; i < array.length; i++) {
       int count = 0;
       for (int j = 0; j < array.length; j++) {
         if (array[i]==array[j])
             count++;
   }
  if (count >= max)
      max = count;
 }

該程序可以運行,但是速度太慢! 我必須找到更好的解決方案,有人可以幫助我嗎?

我必須在雙數組中查找出現次數最多的元素。 我這樣做是這樣的:

int max = 0;
for (int i = 0; i < array.length; i++) {
       int count = 0;
       for (int j = 0; j < array.length; j++) {
         if (array[i]==array[j])
             count++;
   }
  if (count >= max)
      max = count;
 }

該程序可以運行,但是速度太慢! 我必須找到更好的解決方案,有人可以幫助我嗎?

我必須在雙數組中查找出現次數最多的元素。 我這樣做是這樣的:

int max = 0;
for (int i = 0; i < array.length; i++) {
       int count = 0;
       for (int j = 0; j < array.length; j++) {
         if (array[i]==array[j])
             count++;
   }
  if (count >= max)
      max = count;
 }

該程序可以運行,但是速度太慢! 我必須找到更好的解決方案,有人可以幫助我嗎?

我必須在雙數組中查找出現次數最多的元素。 我這樣做是這樣的:

int max = 0;
for (int i = 0; i < array.length; i++) {
       int count = 0;
       for (int j = 0; j < array.length; j++) {
         if (array[i]==array[j])
             count++;
   }
  if (count >= max)
      max = count;
 }

該程序可以運行,但是速度太慢! 我必須找到更好的解決方案,有人可以幫助我嗎?

我必須在雙數組中查找出現次數最多的元素。 我這樣做是這樣的:

int max = 0;
for (int i = 0; i < array.length; i++) {
       int count = 0;
       for (int j = 0; j < array.length; j++) {
         if (array[i]==array[j])
             count++;
   }
  if (count >= max)
      max = count;
 }

該程序可以運行,但是速度太慢! 我必須找到更好的解決方案,有人可以幫助我嗎?

int[] array = new int[] { 1, 2, 4, 1, 3, 4, 2, 2, 1, 5, 2, 3, 5 };

Long max = Arrays.stream(array).boxed().collect(Collectors.groupingBy(i -> i, Collectors.counting())).values()
                .stream().max(Comparator.comparing(Function.identity())).orElse(0L);
    

使用 java 8 在數組中查找出現次數最多的元素如下所示:

final Long maxOccurrencesElement = arr.stream()
                .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
                .entrySet()
                .stream()
                .max((o1, o2) -> o1.getValue().compareTo(o2.getValue()))
                .get()
                .getKey();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM