I need to count the occurrences of each symbol in two different arrays and create a new array to hold the number of these occurrences. I have no idea where to start.
I'm given 3 arrays to begin:
char[] symbols = {'0','1','2','3','4','5','6'};
char[] hiddenCode = {'1','2','3','4'};
char[] guess = {'1','1','4','4'};
The actual instructions state:
Count the number of each symbol in the hiddencode
, and separately count the number of each symbol
in the guess. For each symbol
, determine the minimum of the count of that symbol
in the hiddenCode
and the count of that symbol
found in the guess
. The total number of hits, black and white, is the sum of these minimums for all the symbols
.
Suggestion:
To do the count, create an array of int the length of the number of symbols
. For each symbol
use the indexOf
method you wrote to determine the index in the array to increment the symbols count.
And this is what I was given/have:
public static int countAllHits(char[] hiddenCode, char[] guess, char[] symbols) {
int hits = 0;
int [] arrayCount = new int[symbols.length];
return hits;
}
I've already written an indexOf
method for this program that returns the index of the first occurrence of the specified character:
public static int indexOf(char[] arr, char ch)
{
int index = -1;
if (arr == null || arr.length == 0) {
return index;
}
for (int i = 0; i < arr.length; ++i) {
if (arr[i] == ch) {
index = i;
break;
}
}
return index;
}
And if implemented correctly my new array, using the 3 arrays listed in the beginning, would store {'2', '0', '0', '2'}
because the first symbol appeared twice and the last also appeared twice.
So I need to write a method that used the 3 arrays above that are passed in along with my indexOf method to count how many occurrences of each symbol there are, and store this in my new array, arrayCount. I've seen other posts similar to counting element occurrences in arrays but they all use concepts much higher than my first-semester computer science course and I don't understand/can't use them for this assignment if I want full credit. Thanks for any response in advance!
It was suggested my question is a duplicate, I looked at the other question and it does not help me- that seems to suggest using a hash? I have no clue what that is.