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}

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Starter code: class Solution:
    def sumOfDistinct(self, arr, n):
        # Your code goes here

Solutions:
class Solution:

	def sumOfDistinct(self, arr, n):
		s = set(arr)
		l = list(s)
		return sum(l)


class Solution:

	def sumOfDistinct(self, arr, n):
		return sum(list(set(arr)))


class Solution:

	def sumOfDistinct(self, arr, n):
		a = set(arr)
		b = list(a)
		sum = 0
		for i in range(len(b)):
			sum += b[i]
		return sum


class Solution:

	def sumOfDistinct(self, arr, n):
		x = set(arr)
		y = list(x)
		y.sort()
		s = 0
		for i in range(len(y)):
			s += y[i]
		return s


class Solution:

	def sumOfDistinct(self, arr, n):
		x = set(arr)
		return sum(x)


class Solution:

	def sumOfDistinct(self, arr, n):
		a = set(arr)
		return sum(a)


class Solution:

	def sumOfDistinct(self, arr, n):
		b = set(a)
		count = 0
		for i in b:
			count += i
		return count


class Solution:

	def sumOfDistinct(self, arr, n):
		d = {}
		for i in arr:
			if i in d:
				continue
			else:
				d[i] = 1
		return sum(d.keys())


class Solution:

	def sumOfDistinct(self, arr, n):
		arr1 = set(arr)
		return sum(arr1)


class Solution:

	def sumOfDistinct(self, arr, n):
		arr = list(set(arr))
		sumi = 0
		for i in arr:
			sumi += i
		return sumi


class Solution:

	def sumOfDistinct(self, arr, n):
		a = set(arr)
		b = list(a)
		return sum(b)


class Solution:

	def sumOfDistinct(self, arr, n):
		s1 = set()
		for i in range(len(arr)):
			if arr[i] not in s1:
				s1.add(arr[i])
		return sum(list(s1))


class Solution:

	def sumOfDistinct(self, arr, n):
		res = set()
		for i in arr:
			res.add(i)
		S = 0
		for i in res:
			S = S + i
		return S


class Solution:

	def sumOfDistinct(self, arr, n):
		set_arr = set(arr)
		list_arr = list(set_arr)
		return sum(list_arr)


class Solution:

	def sumOfDistinct(self, arr, n):
		sum = 0
		ans = set(arr)
		for i in ans:
			sum = sum + i
		return sum


class Solution:

	def sumOfDistinct(self, arr, n):
		distinct_sum = 0
		for i in range(n):
			index = abs(arr[i]) - 1
			if arr[index] >= 0:
				distinct_sum += index + 1
				arr[index] = arr[index] * -1
		return distinct_sum


class Solution:

	def sumOfDistinct(self, arr, n):
		s = set(arr)
		sum_s = sum(s)
		return sum_s


class Solution:

	def sumOfDistinct(self, arr, n):
		d = {}
		for i in arr:
			if i not in d:
				d[i] = 1
			else:
				d[i] += 1
		dk = list(d.keys())
		return sum(dk)


class Solution:

	def sumOfDistinct(self, arr, n):
		s = set()
		for i in arr:
			s.add(i)
		return sum(s)


class Solution:

	def sumOfDistinct(self, arr, n):
		sum = 0
		for number in set(arr):
			sum += number
		return sum


class Solution:

	def sumOfDistinct(self, arr, n):
		s = set(arr)
		s = sorted(s)
		return sum(s)


class Solution:

	def sumOfDistinct(self, arr, n):
		freq = {}
		sum = 0
		for i in range(0, n):
			if arr[i] in freq:
				i += 1
			else:
				freq[arr[i]] = 1
		for (k, v) in freq.items():
			if v == 1:
				sum += k
		return sum


class Solution:

	def sumOfDistinct(self, arr, n):
		count = 0
		for i in sorted(set(arr)):
			count += i
		return count


class Solution:

	def sumOfDistinct(self, arr, n):
		A = set(arr)
		S = sum(A)
		return S


class Solution:

	def sumOfDistinct(self, arr, n):
		li = set(arr)
		bi = list(li)
		sum = 0
		for i in range(len(bi)):
			sum += bi[i]
		return sum


class Solution:

	def sumOfDistinct(self, arr, n):
		a = set(arr)
		c = 0
		for i in a:
			c += i
		return c


class Solution:

	def sumOfDistinct(self, arr, n):
		l = list(set(arr))
		s = 0
		for i in l:
			s += i
		return s


class Solution:

	def sumOfDistinct(self, arr, n):
		x = set(arr)
		y = list(x)
		return sum(y)


class Solution:

	def sumOfDistinct(self, arr, n):
		sum = 0
		s = set(arr)
		l = list(s)
		for i in range(len(l)):
			sum = sum + l[i]
		return sum


class Solution:

	def sumOfDistinct(self, arr, n):
		res = {}
		count = 0
		for i in arr:
			if i not in res:
				res[i] = i
				count += i
		return count


class Solution:

	def sumOfDistinct(self, arr: list, n: int) -> int:
		result = 0
		seen = set()
		for i in range(n):
			if arr[i] not in seen:
				result += arr[i]
				seen.add(arr[i])
		return result


class Solution:

	def sumOfDistinct(self, arr, n):
		d = {}
		for i in arr:
			if i not in d:
				d[i] = 1
			else:
				d[i] += 1
		sum = 0
		for (key, value) in d.items():
			sum += key
		return sum


class Solution:

	def sumOfDistinct(self, arr, n):
		k = set(arr)
		t = 0
		for i in k:
			t += i
		return t


class Solution:

	def sumOfDistinct(self, arr, n):
		result = sum(set(arr))
		return result


class Solution:

	def sumOfDistinct(self, arr, n):
		sm = 0
		for i in arr:
			if arr[abs(i) - 1] >= 0:
				sm += abs(i)
				a[abs(i) - 1] *= -1
		return sm


import math

class Solution:

	def sumOfDistinct(self, arr, n):
		sum1 = 0
		for i in range(n):
			if arr[abs(arr[i]) - 1] >= 0:
				sum1 += abs(arr[i])
				arr[abs(arr[i]) - 1] = arr[abs(arr[i]) - 1] * -1
		return sum1


class Solution:

	def sumOfDistinct(self, arr, n):
		value = -1
		sum = 0
		s = set(arr)
		for value in s:
			sum += value
		return sum


class Solution:

	def sumOfDistinct(self, arr, n):
		s = 0
		d = {}
		for i in arr:
			if i not in d:
				d[i] = 0
			d[i] += 1
		for i in a:
			if i in d:
				s += i
				del d[i]
		return s


class Solution:

	def sumOfDistinct(self, arr, n):
		arr = set(arr)
		summ = 0
		for i in arr:
			summ += i
		return summ


class Solution:

	def sumOfDistinct(self, arr, n):
		ar = [*set(arr)]
		s = 0
		for i in ar:
			s = s + i
		return s


class Solution:

	def sumOfDistinct(self, arr, n):
		p = set(arr)
		return sum(p)


class Solution:

	def sumOfDistinct(self, arr, n):
		temp = set(arr)
		return sum(temp)


class Solution:

	def sumOfDistinct(self, arr, n):
		ans = set(arr)
		newAns = list(ans)
		return sum(newAns)