count number of elements in array c++ in o(n) code example
Example: mark occurances of elements in array cpp
int sumOfDistinct(int a[], int n){
int sum = 0;
for (int i = 0; i < n; i++) {
// If element appears first time
if (a[abs(a[i]) - 1] >= 0) {
sum += abs(a[i]);
a[abs(a[i]) - 1] *= -1;
}
}
return sum;