我需要做作业才能获得阵列中最“流行”的数字(最高频率的数字),如果有多个数字具有相同数量的节目,请随机获取一些数字。 经过三个多小时的尝试,并在网上搜索,这就是我得到的:
public int getPopularNumber(){
int count = 1, tempCount;
int popular = array[0];
int temp = 0;
for ( int i = 0; i < (array.length - 1); i++ ){
if ( _buses[i] != null )
temp = array[i];
tempCount = 0;
for ( int j = 1; j < _buses.length; j++ ){
if ( array[j] != null && temp == array[j] )
tempCount++;
}
if ( tempCount > count ){
popular = temp;
count = tempCount;
}
}
return popular;
}
此代码有效,但未考虑重要情况 - 如果有多个具有相同节目数的数字。然后它就是第一个。
例如:int[]a = {1, 2, 3, 4, 4, ,5 ,4 ,5 ,5};
代码将首先显示为4,并且它不应该是随机的。
另一件事 - 因为它是家庭作业我不能使用ArrayList / maps和我们仍然没有学习的东西。
任何帮助将不胜感激。
答案 0 :(得分:6)
由于他们没有给你任何时间复杂性边界,你可以通过扫描阵列N ^ 2次来“暴力破解”问题。 (免责声明,这是最直观的方式,在内存和CPU方面不是最快或最有效的。)
这是一些伪代码:
i
,迭代原始数组,并在每次扫描找到存储在{{1}中的值的重复项时,在i
处将事件数组中的元素递增在原始数组中。通过这种方式,您可以模仿仅使用另一个数组的地图。
答案 1 :(得分:1)
如果您不被允许使用收藏,那么您可以尝试以下代码:
public int getPopularNumber(){
int inputArr[] = {1, 2, 3, 4, 4, 5 ,4 ,5 ,5}; // given input array
int[] tempArr = new int[inputArr.length];
int[] maxValArr = new int[inputArr.length];
// tempArr will have number as index and count as no of occurrence
for( int i = 0 ; i < inputArr.length ; i++){
tempArr[inputArr[i]]++;
}
int maValue = 0;
// find out max count of occurrence (in this case 3 for value 4 and 5)
for( int j = 0 ; j < tempArr.length ; j++){
maValue = Math.max(maValue, tempArr[j]);
}
int l =0;
// maxValArr contains all value having maximum occurrence (in this case 4 and 5)
for( int k = 0 ; k < tempArr.length ; k++){
if(tempArr[k] == maValue){
maxValArr[l] = k;
l++;
}
}
return maxValArr[(int)(Math.random() * getArraySize(maxValArr))];
}
private int getArraySize(int[] arr) {
int size = 0;
for( int i =0; i < arr.length ; i++){
if(arr[i] == 0){
break;
}
size++;
}
return size;
}
答案 2 :(得分:1)
int mostPopNumber =0;
int tmpLastCount =0;
for (int i = 0; i < array.length-1; i++) {
int tmpActual = array[i];
int tmpCount=0;
for (int j = 0; j < array.length; j++) {
if(tmpActual == array[j]){
tmpCount++;
}
}
// >= for the last one
if(tmpCount > tmpLastCount){
tmpLastCount = tmpCount;
mostPopNumber = tmpActual;
}
}
return mostPopNumber;
-
编辑 - 经过多年,多年:D,效果很好:) 我使用了2D int 和 Integer 数组 - 你也可以只使用int数组,但你必须制作更多的长度数组并复制实际值,Integer有默认值null,所以更快 享受哈,你的代码给了我一个想法 - 你不能记住最后一个最受欢迎的号码,顺便说一句,我发现它在那里解决了Find the most popular element in int[] array :)
public static void main(String[] args) {
//income array
int[] array= {1,1,1,1,50,10,20,20,2,2,2,2,20,20};
//associated unique numbers with frequency
int[][] uniQFreqArr = getUniqValues(array);
//print uniq numbers with it's frequency
for (int i = 0; i < uniQFreqArr.length; i++) {
System.out.println("Number: " + uniQFreqArr[i][0] + " found : " + uniQFreqArr[i][1]);
}
//get just most frequency founded numbers
int[][] maxFreqArray = getMaxFreqArray(uniQFreqArr);
//print just most frequency founded numbers
System.out.println("Most freq. values");
for (int i = 0; i < maxFreqArray.length; i++) {
System.out.println("Number: " + maxFreqArray[i][0] + " found : " + maxFreqArray[i][1]);
}
//get some of found values and print
int[] result = getRandomResult(maxFreqArray);
System.out.println("Found most frequency number: " + result[0] + " with count: " + result[1]);
}
//get associated array with unique numbers and it's frequency
static int[][] getUniqValues(int[] inArray){
//first time sort array
Arrays.sort(inArray);
//default value is null, not zero as in int (used bellow)
Integer[][] uniqArr = new Integer[inArray.length][2];
//counter and temp variable
int currUniqNumbers=1;
int actualNum = inArray[currUniqNumbers-1];
uniqArr[currUniqNumbers-1][0]=currUniqNumbers;
uniqArr[currUniqNumbers-1][1]=1;
for (int i = 1; i < inArray.length; i++) {
if(actualNum != inArray[i]){
uniqArr[currUniqNumbers][0]=inArray[i];
uniqArr[currUniqNumbers][1]=1;
actualNum = inArray[i];
currUniqNumbers++;
}else{
uniqArr[currUniqNumbers-1][1]++;
}
}
//get correctly lengthed array
int[][] ret = new int[currUniqNumbers][2];
for (int i = 0; i < uniqArr.length; i++) {
if(uniqArr[i][0] != null){
ret[i][0] = uniqArr[i][0];
ret[i][1] = uniqArr[i][1];
}else{
break;
}
}
return ret;
}
//found and return most frequency numbers
static int[][] getMaxFreqArray(int[][] inArray){
int maxFreq =0;
int foundedMaxValues = 0;
//filter- used sorted array, so you can decision about actual and next value from array
for (int i = 0; i < inArray.length; i++) {
if(inArray[i][1] > maxFreq){
maxFreq = inArray[i][1];
foundedMaxValues=1;
}else if(inArray[i][1] == maxFreq){
foundedMaxValues++;
}
}
//and again copy to correctly lengthed array
int[][] mostFreqArr = new int[foundedMaxValues][2];
int inArr= 0;
for (int i = 0; i < inArray.length; i++) {
if(inArray[i][1] == maxFreq){
mostFreqArr[inArr][0] = inArray[i][0];
mostFreqArr[inArr][1] = inArray[i][1];
inArr++;
}
}
return mostFreqArr;
}
//generate number from interval and get result value and it's frequency
static int[] getRandomResult(int[][] inArray){
int[]ret=new int[2];
int random = new Random().nextInt(inArray.length);
ret[0] = inArray[random][0];
ret[1] = inArray[random][1];
return ret;
}
答案 3 :(得分:-2)
您是否可以使用字符串操作而不是地图/列表? :d
int[] arr = new int[] { 10, 5, 2, 4, 9, 10, 4, 4, 8, 7, 10};
String highest = ""; // Collecting all highest numbers in a String
int highestCount = 0;
for (int a : arr) {
int count = 0;
for (int b : arr) {
if (b == a)
count++; // Getting the number of the current value
}
if (count > highestCount) {
highest = ":" + a + ":"; // new highest count found, overwriting before counted and collected values
highestCount = count;
} else
if (count == highestCount && !highest.contains(":" + a + ":")) { // if value is not in string yet.
highest += ":" + a + ":"; // values are wrapped in :x: to prevent ":xy:".contains("x") false positive
}
}
highest = highest.replaceAll("::", ":").replaceAll("^:", "")
.replaceAll(":$", ""); // remove first and last ":" and replace :: by :
String[] highestNumbers = highest.split(":"); // split string by :
highest = highest.replaceAll(":", ", "); // optional for output: replace : by ,
int randomOutput = Integer.parseInt(highestNumbers[(int) (Math.random()
* (highestNumbers.length - 1))]); // Get random number string and parse to Integer
System.out.println("Highest count: " + highestCount + " for Number(s): "
+ highest + " Random number: " + randomOutput); // output