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

1.2

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

Examples:

Input  : arr[] = {12, 10, 9, 45, 2, 10, 10, 45,10};
Output : 78
Here we take 12, 10, 9, 45, 2 for sum
because it's distinct elements 

Input : arr[] = {1, 10, 9, 4, 2, 10, 10, 45 , 4};
Output : 71

A Simple Solution is to use two nested loops. The outer loop picks an element one by one starting from the leftmost element. The inner loop checks if the element is present on left side of it. If present, then ignores the element.

Time Complexity : O(n2)
Auxiliary Space : O(1)

A Better Solution of this problem is that using sorting technique we firstly sort all elements of array in ascending order and and find one by one distinct elements in array.

// C++ Find the sum of all non-repeated
// elements in an array
#include<bits/stdc++.h>
using namespace std;

// Find the sum of all non-repeated elements
// in an array
int findSum(int arr[], int n)
{
    // sort all elements of array
    sort(arr, arr + n);

    int sum = 0;
    for (int i=0; i<n; i++)
    {
        if (arr[i] != arr[i+1])
            sum = sum + arr[i];
    }

    return sum;
}

// Driver code
int main()
{
    int arr[] = {1, 2, 3, 1, 1, 4, 5, 6};
    int n = sizeof(arr)/sizeof(int);
    cout << findSum(arr, n);
    return 0;
}

Output:

21

Time Complexity : O(n log n)
Space Complexity : O(1)

An Efficient solution of this problem is that using unordered_set we run a single for loop and which value comes first time its add in sum variable and store in hash table that for next time we not use this value.

// C++ Find the sum of all non- repeated
// elements in an array
#include<bits/stdc++.h>
using namespace std;

// Find the sum of all non-repeated elements
// in an array
void findSum(int arr[],int n)
{
    int sum = 0;

    // Hash to store all element of array
    unordered_set< int > s;
    for (int i=0; i<n; i++)
    {
        if (s.find(arr[i]) == s.end())
        {
            sum += arr[i];
            s.insert(arr[i]);
        }
    }

    return sum;
}

// Driver code
int main()
{
    int arr[] = {1, 2, 3, 1, 1, 4, 5, 6};
    int n = sizeof(arr)/sizeof(int);
    cout << findSum(arr, n);
    return 0;
}

Output:

21

Time Complexity : O(n)
Auxiliary Space : O(n)

This article is contributed by DANISH_RAZA. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



1.2 Average Difficulty : 1.2/5.0
Based on 19 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.