0

I have just started programming in java, and I was trying some stuff out. I had written some code for creating my own array with x indexes that I could fill in as the program runs. So if I run the program I could say x = 5 and I would have 5 indexes to fill in (e.g. 5, 2, 7, 4 and 7). The program would then find the max value and print it. I was then wondering if I could have my program print the number of times my maxValue was in the array. In the example above it would be two. I just can't seem to find out how to do this though.

This is the code I have so far:

import java.util.*;

public class oefeningen {

static void maxValue(int[] newArray){//this method decides the largest number in the array

    int result = newArray[0];
    for (int i=1; i<newArray.length; i++){
        if (newArray[i] > result){
            result = newArray[i];
        }
    }
        System.out.println("The largest number is: " +result);
}

public static void main(String[] args) {

    Scanner keyboard = new Scanner(System.in);

    int x; //this is the main part of the array
    System.out.println("Please enter size of array:");
    x = keyboard.nextInt();
    int[] newArray = new int[x];

    for (int j=1; j<=x; j++){//this bit is used for manually entering numbers in the array
        System.out.println("Please enter next value:");
        newArray[j-1] = keyboard.nextInt();
    }
    maxValue(newArray);
}
}
Roan O'Brien
  • 1
  • 1
  • 1
  • 1
    add a counter variable that you incrment when `newArray[i] == result` and reset to 1 when you change result to something else. – OH GOD SPIDERS Jul 04 '17 at 15:54
  • Add a counter and increment it each time you find an element equal to the current max. Reset it to 1 each time the current max changes. You should be able to figure that out by yourself – JB Nizet Jul 04 '17 at 15:55
  • Not sure how this is a duplicate of the other question... It seems that this question is asking "How can I count the number of items that equal the max value?" - The linked question does not have anything to do with counting elements... – pacifier21 Jul 04 '17 at 16:15

2 Answers2

1

You could keep track within your maxValue function, and reset the counter every time a new max is discovered. Something like this:

static void maxValue(int[] newArray){//this method decides the largest number in the array

    int count = 0;
    int result = newArray[0];
    for (int i=1; i<newArray.length; i++){
        if (newArray[i] > result){
            result = newArray[i];
            // reset the count
            count = 1;
        }
        // Check for a value equal to the current max
        else if (newArray[i] == result) {
             // increment the count when you find another match of the current max
             count++;
        }
    }
    System.out.println("The largest number is: " +result);
    System.out.println("The largest number appears " + count + " times in the array");
}
pacifier21
  • 813
  • 1
  • 5
  • 13
0

Just pass array and any value to count how many times it appears in array

public int checkAmountOfValuesInArray(int[] array, int val) {
        int count = 0;
        for (int i = 0; i < array.length; i++) {
            if (array[i]==val) count++;
        }
        return count;
}

or if you want to do everything in one loop:

static void maxValue(int[] newArray) {//this method decides the largest number in the array
        int result = newArray[0];
        int count = 1;

        for (int i = 1; i < newArray.length; i++) {
            if (newArray[i] > result) {
                result = newArray[i];
                count = 1;
            } else if (newArray[i] == result) {
                count++;
            }
        }
        System.out.println("The largest number is: " + result+ ", repeated: " + count + " times");
    }
Vitaliy Moskalyuk
  • 2,463
  • 13
  • 15