Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting an Occurrence in an Array (Java)

Tags:

java

arrays

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 the array and see if it appears elsewhere in the array. The idea is to output the following:

The user is asked to enter 10 integers and those integers are assigned to an array (hence the "numbers" as a paremeter for the method). Let's say I enter "1, 1, 2, 3, 3, 4, 5, 6, 7, 8." The printed results should be "1 occurs 2 times. 2 occurs 1 time. 3 occurs 2 times. 4 occurs 1 time. 5 occurs 1 time. 6 occurs 1 time. 7 occurs 1 time. 8 occurs 1 time." This printing will be done in a separate method.

Everything in my code works except for this method that I created to count the occurrences.

public static int getOccurrences(int[] numbers)
{
    int count = 0;

    for (int i = 0; i < numbers.length; i++)
    {
        int currentInt = numbers[i];;

        if (currentInt == numbers[i])
        {
            count++;
        }
    }

    return count;
}

I know what the issue is here. I set the current integer element in the array to the variable currentInt. The if statement counts every integer element in the array, hence the output being "[I@2503dbd3 occurs 10 times."

How do I keep track of the occurrences of each element in the array?

like image 605
FrakkinShip Avatar asked Apr 14 '15 02:04

FrakkinShip


People also ask

How do you count occurrences of an element in an array?

To count the occurrences of each element in an array:Declare a variable that stores an empty object. Use the for...of loop to iterate over the array. On each iteration, increment the count for the current element if it exists or initialize the count to 1 .


1 Answers

package countoccurenceofnumbers;

import java.util.Scanner;
public class CountOccurenceOfNumbers {


    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int [] num = new int[100]; 
        int [] count = new int[100];
        //Declare counter variable i
        //and temp variable that will
        //temporarily hold the value
        //at a certain index of num[] array
        int i,temp = 0;
        System.out.println("Enter the integers between 1 and 100: ");

        //Initialize num[] array with user input
        for(i=0; i < num.length; i++){
            num[i] = input.nextInt();
            //expected input will end when user enters zero
            if(num[i] == 0){
                break;
            }
        }//end of for loop

        //value at a given index of num array 
        //will be stored in temp variable
        //temp variable will act as an index value
        //for count array and keep track of number
        //of occurences of each number
        for(i = 0; i < num.length; i++){
                temp = num[i];
                count[temp]++;
            }//end of for looop

        for(i=1; i < count.length; i++){

            if(count[i] > 0 && count[i] == 1){
             System.out.printf("%d occurs %d time\n",i, count[i]);
             }
            else if(count[i] >=2){
                System.out.printf("%d occurs %d times\n",i, count[i]);
            }


         }//end of for loop

    }//end of main
    }//end of CountOccurrenceOfNumbers

///////////OUTPUT//////////////////////

Enter the integers between 1 and 100:
2 5 6 5 4 3 23 43 2 0
2 occurs 2 times
3 occurs 1 time
4 occurs 1 time
5 occurs 2 times
6 occurs 1 time
23 occurs 1 time
43 occurs 1 time
BUILD SUCCESSFUL (total time: 3 minutes 23 seconds)

like image 139
Brenda Mejia Avatar answered Nov 07 '22 09:11

Brenda Mejia