You can use a HashMap to serve the purpose.
In the HashMap you can store the elements and the count of it.
Here is a simple implementation I used.
import java.util.HashMap;
import java.util.Scanner;
public class CountElement
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter the number of elements in the Array");
int n = scan.nextInt();
int arr[] = new int [n];
System.out.println("Enter "+n+" elements");
for(int i=0; i<n; i++)
{
arr[i] = scan.nextInt();
}
HashMap<Integer,Integer> countMap = new HashMap<Integer, Integer>();
for (int i : arr)
{
if (countMap.containsKey(i))
{
countMap.put(i, countMap.get(i)+1);
}
else
{
countMap.put(i, 1);
}
}
System.out.println(countMap);
}
}
Output:
Enter the number of elements in the Array
7
Enter 7 elements
1 2 1 1 2 3 4
{1=3, 2=2, 3=1, 4=1}
Hope this will suffice your work.