# SUM OF UNIQUE ELEMENTS IN AN ARRAY

**Approach 1:**

1.Input each element of the array.

2.Initialize a variable sum to 0.

3.For each element in the array check whether that number is repeated.

– If no add the number to the sum.

4. Output sum.

Time complexity of this algorithm is N^2.

#include

int main(){

int arr[1000],i,j,sum=0,n;

printf("Enter number of elements \n");

scanf("%d",&n);

printf("Enter elements of the arrayn");

for(i=0;i

The same could be solved in time less than N^2

**Approach 2:**

1. Decide the upper limit of your input number. In the code MAX taken in 1000. Now all numbers should be less than 1000.

2. Create a variable sum and initialize to 0.

3. Create an array of size 1000. Say array(arr) and initialize it to 0

4. For each element increment arr[element]

For example suppose input is 4 then increment arr[4].

Fig. shows this for numbers less than 10.

5. Iterate over the array arr and if value is 1 add it's index value to sum.

In the fig. the circled elements have value 1 and so it's index which is highlighted would be added.

6. Output sum

In figure it's assumed that numbers are less than 10.

Time complexity of this algorithm is N.

#include

#define MAX 1000

int main(){

int arr[MAX]={},i,j,sum=0,n,input;

printf("Enter number of elementsn");

scanf("%d",&n);

printf("Enter elements of the arrayn");

for(i=0;i