count the number of times an element occurs in an array java code example
Example 1: mark occurances of elements in array cpp
int sumOfDistinct(int a[], int n){
int sum = 0;
for (int i = 0; i < n; i++) {
if (a[abs(a[i]) - 1] >= 0) {
sum += abs(a[i]);
a[abs(a[i]) - 1] *= -1;
}
}
return sum;
Example 2: get ocurrences in array java
List asList = Arrays.asList(array);
Set<String> mySet = new HashSet<String>(asList);
for(String s: mySet){
System.out.println(s + " " +Collections.frequency(asList,s));
}