int[] a = new int[10]{1,2,3,4,5,6,7,7,7,7};
如何编写方法并返回7?
我想在没有列表,地图或其他帮助程序的帮助下将其保持为原生。仅数组[]。
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; }