gpt4 book ai didi

java - 如何找到重复/重复的数组值并将其显示在输出中?

转载 作者:行者123 更新时间:2023-12-01 23:11:12 25 4
gpt4 key购买 nike

我已经尝试了以下程序,但我遇到了困难,请帮助我。下面是我的程序

import java.util.ArrayList;

public class PrintNosandRepetition

{

public static void main(String[] args)

{

int a[] = new int[] {1,3,4,5,6,3,2,4,6,7,9,4,12,3,4,6,8,9,7,6,43,2,4,7,7,5,2,1,3,4,6,311,1};

for (int i=0; i< a.length; i++){

System.out.print(a[i]+ " ");

}
for (i=1, j<a.length; j++)


}

输出必须是流行的“1-重复3次”等

最佳答案

可以对原始数组进行排序,然后循环遍历,逐个扫描元素。这将在 O(nlogn) 中运行.

或者您可以使用Map<Integer, Integer>它将存储每个数字出现的次数。该解决方案运行于 O(n)但使用额外的内存。

关于java - 如何找到重复/重复的数组值并将其显示在输出中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21944609/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com