Title sums it up. For example {2,3,3,3,1}
would return 3, {2,2,4,4,4}
would return 2; In my code I only get the biggest number that occurs n times. If 3 would occur 2 times and 2 would occur 2 times the program should return 2 but in my code it return 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
. 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?
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). 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. 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. Whenever you find that counter >= n
, return n
. If not earlier, it will happen when n
is 0, since 0 is always present 0 times.
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
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
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;
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;
} }`
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.