I am completely stumped. I took a break for a few hours and I can\'t seem to figure this one out. It\'s upsetting!
I know that I need to check the current element in
@NYB You're almost right but you have to output the count value and also start it from zero on each element check.
int count=0,currentInt=0;
for (int i = 0; i < numbers.length; i++)
{
currentInt = numbers[i];
count=0;
for (int j = 0; j < numbers.length; j++)
{
if (currentInt == numbers[j])
{
count++;
}
}
System.out.println(count);
}
@loikkk I tweaked your code a bit for print out of occurrence for each element.
int[] a = { 1, 9, 8, 8, 7, 6, 5, 4, 3, 3, 2, 1 };
Arrays.sort(a);
int nbOccurences = 1;
for (int i = 0, length = a.length; i < length; i++) {
if (i < length - 1) {
if (a[i] == a[i + 1]) {
nbOccurences++;
}
} else {
System.out.println(a[i] + " occurs " + nbOccurences
+ " time(s)"); //end of array
}
if (i < length - 1 && a[i] != a[i + 1]) {
System.out.println(a[i] + " occurs " + nbOccurences
+ " time(s)"); //moving to new element in array
nbOccurences = 1;
}
}