mark occurances of elements in array cpp

C++
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;
Source

Also in C++: