Array Mode Calculation Logic
Which of the following can replace /* missing condition 1 / and / missing condition 2 */ so the code segment works as intended?
Consider the mode method, which is intended to return the most frequently occurring value (mode) in its int[] parameter arr. For example, if the parameter of the mode method has the contents {6, 5, 1, 5, 2, 6, 5}, then the method is intended to return 5.
/** Precondition: arr.length >= 1 */
public static int mode(int[] arr)
{
int modeCount = 1;
int mode = arr[0];
for (int j = 0; j < arr.length; j++)
{
int valCount = 0;
for (int k = 0; k < arr.length; k++)
{
if ( /* missing condition 1 */ )
{
valCount++;
}
}
if ( /* missing condition 2 */ )
{
modeCount = valCount;
mode = arr[j];
}
}
return mode;
}
Which of the following can replace /* missing condition 1 */ and /* missing condition 2 */ so the code segment works as intended?
A
arr[j] != arr[k] | modeCount > valCount
B
arr[j] == arr[k] | valCount > modeCount
C
arr[j] != arr[k] | modeCount != valCount
D
arr[j] == arr[k] | modeCount > valCount
Question Leaderboard
| Rank | |||||
|---|---|---|---|---|---|
| #1 | kaisuki | 1 | 1 | 0m 00s | 100 |
| #2 | singhris000 | 1 | 1 | 1m 00s | 40 |
| #3 | rkim5100933 | 1 | 2 | 6m 11s | -281 |
| #4 | y.seong2027 | 1 | 1 | 85h 05m | -306,236 |
APFIVE