![在int []数组中找到最受欢迎的元素,第1张 在int []数组中找到最受欢迎的元素,第1张](/aiimages/%E5%9C%A8int+%5B%5D%E6%95%B0%E7%BB%84%E4%B8%AD%E6%89%BE%E5%88%B0%E6%9C%80%E5%8F%97%E6%AC%A2%E8%BF%8E%E7%9A%84%E5%85%83%E7%B4%A0.png)
public int getPopularElement(int[] a){ int count = 1, tempCount; int popular = a[0]; int temp = 0; for (int i = 0; i < (a.length - 1); i++) { temp = a[i]; tempCount = 0; for (int j = 1; j < a.length; j++) { if (temp == a[j]) tempCount++; } if (tempCount > count) { popular = temp; count = tempCount; } } return popular;}欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)