1

So how do I find max and min value of group of numbers. Ex: The numbers are

int num[] = {1,2,3,4,5,6,7,8,9,2,2,2,2,2,};

The next things is how to find out how many times 2 appears in the array. This is what I think.

char a = "2"
int count = 0;
if (num.length = a) {
    count++;
    System.out.print (count);
}
thedon15
  • 33
  • 5

3 Answers3

2

How do I find max and min value of group of numbers.

int num[] = {1,2,3,4,5,6,7,8,9,2,2,2,2,2,};
getMaxValue(num);
getMinValue(num);

// getting the maximum value
public static int getMaxValue(int[] array){  
      int maxValue = array[0];  
      for(int i=1;i < array.length;i++){  
      if(array[i] > maxValue){  
      maxValue = array[i];  

         }  
     }  
             return maxValue;  
}  

// getting the miniumum value
public static int getMinValue(int[] array){  
     int minValue = array[0];  
     for(int i=1;i<array.length;i++){  
     if(array[i] < minValue){  
     minValue = array[i];  
        }  
     }  
    return minValue;  
}  
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
0

For occurences use:

int arr = new int[10]; 
// fill arr with values;
for(int i=0; i<arr.length; i++){
  if(arr[i] == 2){ counter++;}
}
CIsForCookies
  • 12,097
  • 11
  • 59
  • 124
0

Use a TreeMap with key as the Number and value is the number of occurences .Then use firstEntry() and lastEntry() for getting least and maximum value.TreeMap will sort the Integers in ascending order.

TreeMap<Integer,Integer> tuples = new TreeMap<Integer,Integer>();
For occurence of 2 ,simply use Integer tuples.get(2);
Kumar Abhinav
  • 6,565
  • 2
  • 24
  • 35