Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,166 questions

40,722 answers

573 users

How to get the frequency of smallest array value in Java

3 Answers

0 votes
public class MyClass {
    public static int minArr(int[] arr) {
        int minval = arr[0];   
        for (int i = 1; i < arr.length; i++) {
            if (arr[i] < minval) {
                minval = arr[i]; 
            }
        }
        return minval;
    }
    public static void main(String args[]) {
        int[] arr = new int [] {6, 4, 4, 7, 3, 3, 3, 5, 5, 7, 8, 3, 4, 3};    
        
        int frequency = 0;
        
        int min = minArr(arr);
        
        for (int i = 1; i < arr.length; i++) {
            if (arr[i] == min) {
                frequency++;
            }
        }
        System.out.println(frequency);
    }
}
  
  
  
/*
run:
     
5
     
*/

 





answered Aug 4, 2022 by avibootz
edited Aug 4, 2022 by avibootz
0 votes
import java.util.Arrays;

public class MyClass {
    public static void main(String args[]) {
        int[] arr = new int [] {6, 4, 4, 7, 3, 3, 3, 5, 5, 7, 8, 3, 4, 3};  
        
        int frequency = 0;
        
        int min = Arrays.stream(arr).min().getAsInt();
        
        for (int i = 1; i < arr.length; i++) {
            if (arr[i] == min) {
                frequency++;
            }
        }
        System.out.println(frequency);
    }
}
  
  
  
/*
run:
     
5
     
*/

 





answered Aug 4, 2022 by avibootz
edited Aug 4, 2022 by avibootz
0 votes
public class MyClass {
    public static int get_smallest_frequency(int[] arr) {
        int min = arr[0], frequency = 1;
    
        for (int i = 1; i < arr.length; i++) {
            if (arr[i] < min) {
                min = arr[i];
                frequency = 1;
            }
            else if (arr[i] == min)
                frequency++;
        }
    
        return frequency;
    }

    public static void main(String args[]) {
        int[] arr = new int [] {6, 4, 4, 7, 3, 3, 3, 5, 5, 7, 8, 3, 4, 3};    

        System.out.println(get_smallest_frequency(arr));
    }
}
   
   
   
/*
run:
      
5
      
*/

 





answered Aug 17, 2022 by avibootz

Related questions

1 answer 32 views
2 answers 47 views
1 answer 45 views
1 answer 45 views
1 answer 55 views
...