P15267 [GeeksforGeeks] Sum of distinct elements 15115
普及-
题目描述

Given an array, arr[] of N elements such that every element of the array is an integer in the range 1 to n, the task is to find the sum of all the distinct elements of the array.

Example 1:
Input: N = 9
arr[] = {5, 1, 2, 4, 6, 7, 3, 6, 7}
Output: 28
Explanation: The distinct elements in
the array are 1, 2, 3, 4, 5, 6, 7.
Example 2:
Input: N = 3, arr[] = {1, 1, 1}
Output: 1
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function sumOfDistinct() that takes array arr[] and integer N as parameters and returns the sum of a distinct elements of the array.

Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).

Constraints:
1 ≤ N ≤ 10^{6}

输入格式

见题面

输出格式

见题面

样例输入
N = 9 arr[] = {5, 1, 2, 4, 6, 7, 3, 6, 7}
样例输出
28