Find sum of non-repeating (distinct) elements in an array

QUESTION

Given an integer array with repeated elements, the task is to find sum of all distinct elements in array.

“TESTCASE_1”: “5\n2 3 4 6 1\n###—###SEPERATOR—###—\n16”, “TESTCASE_2”: “6\n2 4 6 2 4 6\n###—###SEPERATOR—###—\n0”, “TESTCASE_3”: “7\n2 4 6 5 4 7 8\n###—###SEPERATOR—###—\n28”, “TESTCASE_4”: “9\n5 4 3 1 2 6 1 1 3\n###—###SEPERATOR—###—\n17”, “TESTCASE_5”: “0\n###—###SEPERATOR—###—\n0

ANSWER

#include <iostream>
using namespace std;
int main()
{
  int n,i,j;
  cin>>n;
  int a[n], c, sum=0;
  for(i=0; i<n; i++)
    cin>>a[i];
  for(i=0; i<n; i++)
  {
    c=0;
    for(j=0;j<n; j++)
    {
      if(a[i]==a[j])
        c++;
    }
    if(c==1)
      sum+=a[i];
  }
 cout<<sum;

	return 0;
}
Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock