[英]Get the biggest number "n" in an array that occurs "n" times
Title sums it up.标题总结了它。 For example
{2,3,3,3,1}
would return 3, {2,2,4,4,4}
would return 2;例如
{2,3,3,3,1}
将返回 3, {2,2,4,4,4}
将返回 2; In my code I only get the biggest number that occurs n times.在我的代码中,我只得到出现 n 次的最大数字。 If 3 would occur 2 times and 2 would occur 2 times the program should return 2 but in my code it return 0.
如果 3 会出现 2 次而 2 会出现 2 次,程序应该返回 2 但在我的代码中它返回 0。
public class arrays {
public static int NmalN(int[] arr) {
int max = 0;
int counter = 1;
for (int i=0; i<arr.length;) {
if (arr[i]>max) {
max = arr[i];
i++;
}
else i++;
}
for(int j = 0; j<arr.length; j++) {
if (arr[j]==max) {
counter++;
j++;
}
else {
j++;
}
}
if(max == counter) {
return counter;
}
else return 0;
}
public static void main(String[] args) {
int [] arr = {1,2,3,3,2,};
System.out.println("answer: "+ (NmalN(arr)));
}
}
As I read your code, you find the biggest number in the array, max
. 当我阅读您的代码时,您会在数组
max
找到最大的数字。 If that number occurs exactly max
times, no more no less, you return it, otherwise you return 0. When 3 occurs only twice, you should probably try with 2, then with 1, etc. rather than just returning 0. Also think: is it OK if the number occurs more than max
times? 如果该数字恰好出现在
max
次数上,则返回它,否则返回0。否则,您返回0。当3仅出现两次时,您应该尝试使用2,然后尝试1,以此类推,而不仅仅是返回0。如果次数超过max
次数可以吗?
Your loop for finding max
looks fine (though convention would place i++
inside for (int i=0; i<arr.length; i++)
and not inside the loop). 查找
max
循环看起来不错(尽管约定会将i++
放在里面for (int i=0; i<arr.length; i++)
而不是放在循环内)。 After that you need a for
loop to count down from max
to 0, for each number n
in that range checking if it occurs at least n
times. 之后,您需要一个
for
循环,以从max
倒数到0,对于该范围内的每个数字n
,检查它是否至少出现了n
次。 You way of counting is almost correct, but as Vasan said in a comment, you are doing j++
both in for(int j = 0; j<arr.length; j++)
and in the loop, that is, twice, so remove the one inside the loop. 您的计数方式几乎是正确的,但是正如Vasan在评论中所说,您正在
for(int j = 0; j<arr.length; j++)
和循环中(即两次for(int j = 0; j<arr.length; j++)
都在执行j++
for(int j = 0; j<arr.length; j++)
一个在循环内。 Whenever you find that counter >= n
, return n
. 每当您发现该
counter >= n
,就返回n
。 If not earlier, it will happen when n
is 0, since 0 is always present 0 times. 如果不是更早,它将在
n
为0时发生,因为0总是存在0次。
import java.util.Arrays;
public class find_Biggest_Num_N_In_Array_Occures_N_times {
public static int Solution(int[] ar){
int[] counter= new int[ar.length];
Arrays.fill(counter, 0);
for(int i=0; i<ar.length;i++){
int pos=ar[i];
counter[pos]++;
}
for(int j=ar.length-1;j>=1;j--){
if(counter[j]==j){
return j;
}
}
return -1;
}
public static void main(String args[]){
System.out.println(Solution(new int[]{1,2,2,3,4,4,4,4,3,5,5,5,5,5,1}));
}
}
public static int solution(Integer[] intArray) {
int result = 0;
Arrays.sort(intArray, Collections.reverseOrder());
for (int i=0; i<intArray.length;i++) {
int count = 0;
for(int j=0;j<intArray.length;j++) {
if(intArray[i]==intArray[j]) {
count++;
}
}
if(count == intArray[i]) {
result = intArray[i];
break;
}
}
return result;
}
In javascript, it'll be like this在javascript中,会是这样
function getLargestFrequency(arr) {
let frequency = {};
// Count frequency of each number
for (let i = 0; i < arr.length; i++) {
let num = arr[i];
if (frequency[num]) {
frequency[num]++;
} else {
frequency[num] = 1;
}
}
let maxFrequency = 0;
let maxNum;
for (let num in frequency) {
if (frequency[num] > maxFrequency) {
maxFrequency = frequency[num];
maxNum = num;
}
}
// Initialize the maxFrequencyCount with maxFrequency
let maxFrequencyCount = maxFrequency;
// Iterate over the frequency object
for (let num in frequency) {
if (frequency[num] === maxFrequencyCount) {
// If the current number's frequency count is equal to maxFrequencyCount, return it
return num;
}
}
// If no number with the highest frequency count is found, return 0
return 0;
}
and you can see the output你可以看到 output
let array1 = [2,3,3,3,1];
let result1 = getLargestFrequency(array1);
console.log(result1); // should return 3
let array2 = [2,2,4,4,4];
let result2 = getLargestFrequency(array2);
console.log(result2); // should return 2
I think this should work ` import java.util.Arrays;我认为这应该有效 ` import java.util.Arrays;
public class find_Biggest_Num_N_In_Array_Occures_N_times {
public static int Solution(int[] ar){
Arrays.sort(ar);
int maxElement = ar[ar.length -1];
int[] counter= new int[maxElement + 1];
Arrays.fill(counter, 0);
for(int i=0; i<ar.length;i++){
int pos=ar[i];
counter[pos]++;
}
for(int j=ar.length-1;j>=1;j--){
if(counter[j]==j){
return j;
}
}
return -1;
} }` }`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.