Sum of all distinct elements in array
Here given code implementation process.
// C Program
// Sum of all distinct elements in array
#include <stdio.h>
//Display elements of given array
void printArray(int arr[], int size)
{
for (int i = 0; i < size; ++i)
{
printf(" %d", arr[i]);
}
}
// Find the sum of all distinct element which is exists in given array
void distinctSum(int arr[], int size)
{
// Define auxiliary resultant variables
int sum = 0;
int flag = 0;
// Loop controlling variables
int i = 0;
int head = 0;
int tail = 0;
// Execute loop through by array size
for (i = 0; i < size; ++i)
{
// Decide boundary of inner loop to test repeated element
head = 0;
tail = i - 1;
// Checker which is used to test distinct element
flag = 1;
// Check that [i] elements are used in previously
while (head <= tail && flag == 1)
{
if (arr[head] == arr[i] || arr[tail] == arr[i])
{
// When elements are already taken
flag = 0;
}
else
{
head++;
tail--;
}
}
if (flag == 1)
{
// Add distinct element
sum += arr[i];
}
}
printf("\n Sum of distinct element is : %d\n", sum);
}
int main(int argc, char
const *argv[])
{
// Define array of integer elements
int arr1[] = {
1 , 2 , 7 , 3 , 5 , 7 , 6 , 3
};
int arr2[] = {
0 , 1 , 4 , 2 , 3 , 2 , 4
};
// Get the size
int size = sizeof(arr1) / sizeof(arr1[0]);
printf(" Array elements \n");
printArray(arr1, size);
distinctSum(arr1, size);
// Get the size
size = sizeof(arr2) / sizeof(arr2[0]);
printf("\n Array elements \n");
printArray(arr2, size);
distinctSum(arr2, size);
return 0;
}
Output
Array elements
1 2 7 3 5 7 6 3
Sum of distinct element is : 24
Array elements
0 1 4 2 3 2 4
Sum of distinct element is : 10
/*
Java Program
Sum of all distinct elements in array
*/
public class DistinctElement
{
// Display elements of given array
public void printArray(int[] arr, int size)
{
for (int i = 0; i < size; ++i)
{
System.out.print(" " + arr[i]);
}
}
// Find the sum of all distinct element which is exists in given array
public void distinctSum(int[] arr, int size)
{
// Define auxiliary resultant variables
int sum = 0;
int flag = 0;
// Loop controlling variables
int i = 0;
int head = 0;
int tail = 0;
// Execute loop through by array size
for (i = 0; i < size; ++i)
{
// Decide boundary of inner loop to test repeated element
head = 0;
tail = i - 1;
// Checker which is used to test distinct element
flag = 1;
// Check that [i] elements are used in previously
while (head <= tail && flag == 1)
{
if (arr[head] == arr[i] || arr[tail] == arr[i])
{
// When elements are already taken
flag = 0;
}
else
{
head++;
tail--;
}
}
if (flag == 1)
{
// Add distinct element
sum += arr[i];
}
}
System.out.print("\n Sum of distinct element is : " + sum + "\n");
}
public static void main(String[] args)
{
DistinctElement task = new DistinctElement();
// Define array of integer elements
int[] arr1 = {
1 , 2 , 7 , 3 , 5 , 7 , 6 , 3
};
int[] arr2 = {
0 , 1 , 4 , 2 , 3 , 2 , 4
};
// Get the size
int size = arr1.length;
System.out.print(" Array elements \n");
task.printArray(arr1, size);
task.distinctSum(arr1, size);
// Get the size
size = arr2.length;
System.out.print("\n Array elements \n");
task.printArray(arr2, size);
task.distinctSum(arr2, size);
}
}
Output
Array elements
1 2 7 3 5 7 6 3
Sum of distinct element is : 24
Array elements
0 1 4 2 3 2 4
Sum of distinct element is : 10
// Include header file
#include <iostream>
using namespace std;
/*
C++ Program
Sum of all distinct elements in array
*/
class DistinctElement
{
public:
// Display elements of given array
void printArray(int arr[], int size)
{
for (int i = 0; i < size; ++i)
{
cout << " " << arr[i];
}
}
// Find the sum of all distinct element which is exists in given array
void distinctSum(int arr[], int size)
{
// Define auxiliary resultant variables
int sum = 0;
int flag = 0;
// Loop controlling variables
int i = 0;
int head = 0;
int tail = 0;
// Execute loop through by array size
for (i = 0; i < size; ++i)
{
// Decide boundary of inner loop to test repeated element
head = 0;
tail = i - 1;
// Checker which is used to test distinct element
flag = 1;
// Check that [i] elements are used in previously
while (head <= tail && flag == 1)
{
if (arr[head] == arr[i] || arr[tail] == arr[i])
{
// When elements are already taken
flag = 0;
}
else
{
head++;
tail--;
}
}
if (flag == 1)
{
// Add distinct element
sum += arr[i];
}
}
cout << "\n Sum of distinct element is : " << sum << "\n";
}
};
int main()
{
DistinctElement task = DistinctElement();
// Define array of integer elements
int arr1[] = {
1 , 2 , 7 , 3 , 5 , 7 , 6 , 3
};
int arr2[] = {
0 , 1 , 4 , 2 , 3 , 2 , 4
};
// Get the size
int size = sizeof(arr1) / sizeof(arr1[0]);
cout << " Array elements \n";
task.printArray(arr1, size);
task.distinctSum(arr1, size);
// Get the size
size = sizeof(arr2) / sizeof(arr2[0]);
cout << "\n Array elements \n";
task.printArray(arr2, size);
task.distinctSum(arr2, size);
return 0;
}
Output
Array elements
1 2 7 3 5 7 6 3
Sum of distinct element is : 24
Array elements
0 1 4 2 3 2 4
Sum of distinct element is : 10
// Include namespace system
using System;
/*
C# Program
Sum of all distinct elements in array
*/
public class DistinctElement
{
// Display elements of given array
public void printArray(int[] arr, int size)
{
for (int i = 0; i < size; ++i)
{
Console.Write(" " + arr[i]);
}
}
// Find the sum of all distinct element which is exists in given array
public void distinctSum(int[] arr, int size)
{
// Define auxiliary resultant variables
int sum = 0;
int flag = 0;
// Loop controlling variables
int i = 0;
int head = 0;
int tail = 0;
// Execute loop through by array size
for (i = 0; i < size; ++i)
{
// Decide boundary of inner loop to test repeated element
head = 0;
tail = i - 1;
// Checker which is used to test distinct element
flag = 1;
// Check that [i] elements are used in previously
while (head <= tail && flag == 1)
{
if (arr[head] == arr[i] || arr[tail] == arr[i])
{
// When elements are already taken
flag = 0;
}
else
{
head++;
tail--;
}
}
if (flag == 1)
{
// Add distinct element
sum += arr[i];
}
}
Console.Write("\n Sum of distinct element is : " + sum + "\n");
}
public static void Main(String[] args)
{
DistinctElement task = new DistinctElement();
// Define array of integer elements
int[] arr1 = {
1 , 2 , 7 , 3 , 5 , 7 , 6 , 3
};
int[] arr2 = {
0 , 1 , 4 , 2 , 3 , 2 , 4
};
// Get the size
int size = arr1.Length;
Console.Write(" Array elements \n");
task.printArray(arr1, size);
task.distinctSum(arr1, size);
// Get the size
size = arr2.Length;
Console.Write("\n Array elements \n");
task.printArray(arr2, size);
task.distinctSum(arr2, size);
}
}
Output
Array elements
1 2 7 3 5 7 6 3
Sum of distinct element is : 24
Array elements
0 1 4 2 3 2 4
Sum of distinct element is : 10
<?php
/*
Php Program
Sum of all distinct elements in array
*/
class DistinctElement
{
// Display elements of given array
public function printArray( & $arr, $size)
{
for ($i = 0; $i < $size; ++$i)
{
echo " ". $arr[$i];
}
}
// Find the sum of all distinct element which is exists in given array
public function distinctSum( & $arr, $size)
{
// Define auxiliary resultant variables
$sum = 0;
$flag = 0;
// Loop controlling variables
$i = 0;
$head = 0;
$tail = 0;
// Execute loop through by array size
for ($i = 0; $i < $size; ++$i)
{
// Decide boundary of inner loop to test repeated element
$head = 0;
$tail = $i - 1;
// Checker which is used to test distinct element
$flag = 1;
// Check that [i] elements are used in previously
while ($head <= $tail && $flag == 1)
{
if ($arr[$head] == $arr[$i] || $arr[$tail] == $arr[$i])
{
// When elements are already taken
$flag = 0;
}
else
{
$head++;
$tail--;
}
}
if ($flag == 1)
{
// Add distinct element
$sum += $arr[$i];
}
}
echo "\n Sum of distinct element is : ". $sum ."\n";
}
}
function main()
{
$task = new DistinctElement();
// Define array of integer elements
$arr1 = array(1, 2, 7, 3, 5, 7, 6, 3);
$arr2 = array(0, 1, 4, 2, 3, 2, 4);
// Get the size
$size = count($arr1);
echo " Array elements \n";
$task->printArray($arr1, $size);
$task->distinctSum($arr1, $size);
// Get the size
$size = count($arr2);
echo "\n Array elements \n";
$task->printArray($arr2, $size);
$task->distinctSum($arr2, $size);
}
main();
Output
Array elements
1 2 7 3 5 7 6 3
Sum of distinct element is : 24
Array elements
0 1 4 2 3 2 4
Sum of distinct element is : 10
/*
Node Js Program
Sum of all distinct elements in array
*/
class DistinctElement
{
// Display elements of given array
printArray(arr, size)
{
for (var i = 0; i < size; ++i)
{
process.stdout.write(" " + arr[i]);
}
}
// Find the sum of all distinct element which is exists in given array
distinctSum(arr, size)
{
// Define auxiliary resultant variables
var sum = 0;
var flag = 0;
// Loop controlling variables
var i = 0;
var head = 0;
var tail = 0;
// Execute loop through by array size
for (i = 0; i < size; ++i)
{
// Decide boundary of inner loop to test repeated element
head = 0;
tail = i - 1;
// Checker which is used to test distinct element
flag = 1;
// Check that [i] elements are used in previously
while (head <= tail && flag == 1)
{
if (arr[head] == arr[i] || arr[tail] == arr[i])
{
// When elements are already taken
flag = 0;
}
else
{
head++;
tail--;
}
}
if (flag == 1)
{
// Add distinct element
sum += arr[i];
}
}
process.stdout.write("\n Sum of distinct element is : " + sum + "\n");
}
}
function main()
{
var task = new DistinctElement();
// Define array of integer elements
var arr1 = [1, 2, 7, 3, 5, 7, 6, 3];
var arr2 = [0, 1, 4, 2, 3, 2, 4];
// Get the size
var size = arr1.length;
process.stdout.write(" Array elements \n");
task.printArray(arr1, size);
task.distinctSum(arr1, size);
// Get the size
size = arr2.length;
process.stdout.write("\n Array elements \n");
task.printArray(arr2, size);
task.distinctSum(arr2, size);
}
main();
Output
Array elements
1 2 7 3 5 7 6 3
Sum of distinct element is : 24
Array elements
0 1 4 2 3 2 4
Sum of distinct element is : 10
# Python 3 Program
# Sum of all distinct elements in array
class DistinctElement :
# Display elements of given array
def printArray(self, arr, size) :
i = 0
while (i < size) :
print(" ", arr[i], end = "")
i += 1
# Find the sum of all distinct element which is exists in given array
def distinctSum(self, arr, size) :
# Define auxiliary resultant variables
sum = 0
flag = 0
# Loop controlling variables
i = 0
head = 0
tail = 0
# Execute loop through by array size
while (i < size) :
# Decide boundary of inner loop to test repeated element
head = 0
tail = i - 1
# Checker which is used to test distinct element
flag = 1
# Check that [i] elements are used in previously
while (head <= tail and flag == 1) :
if (arr[head] == arr[i] or arr[tail] == arr[i]) :
# When elements are already taken
flag = 0
else :
head += 1
tail -= 1
if (flag == 1) :
# Add distinct element
sum += arr[i]
i += 1
print("\n Sum of distinct element is : ", sum )
def main() :
task = DistinctElement()
# Define array of integer elements
arr1 = [1, 2, 7, 3, 5, 7, 6, 3]
arr2 = [0, 1, 4, 2, 3, 2, 4]
# Get the size
size = len(arr1)
print(" Array elements ")
task.printArray(arr1, size)
task.distinctSum(arr1, size)
# Get the size
size = len(arr2)
print("\n Array elements ")
task.printArray(arr2, size)
task.distinctSum(arr2, size)
if __name__ == "__main__": main()
Output
Array elements
1 2 7 3 5 7 6 3
Sum of distinct element is : 24
Array elements
0 1 4 2 3 2 4
Sum of distinct element is : 10
# Ruby Program
# Sum of all distinct elements in array
class DistinctElement
# Display elements of given array
def printArray(arr, size)
i = 0
while (i < size)
print(" ", arr[i])
i += 1
end
end
# Find the sum of all distinct element which is exists in given array
def distinctSum(arr, size)
# Define auxiliary resultant variables
sum = 0
flag = 0
# Loop controlling variables
i = 0
head = 0
tail = 0
# Execute loop through by array size
while (i < size)
# Decide boundary of inner loop to test repeated element
head = 0
tail = i - 1
# Checker which is used to test distinct element
flag = 1
# Check that [i] elements are used in previously
while (head <= tail && flag == 1)
if (arr[head] == arr[i] || arr[tail] == arr[i])
# When elements are already taken
flag = 0
else
head += 1
tail -= 1
end
end
if (flag == 1)
# Add distinct element
sum += arr[i]
end
i += 1
end
print("\n Sum of distinct element is : ", sum ,"\n")
end
end
def main()
task = DistinctElement.new()
# Define array of integer elements
arr1 = [1, 2, 7, 3, 5, 7, 6, 3]
arr2 = [0, 1, 4, 2, 3, 2, 4]
# Get the size
size = arr1.length
print(" Array elements \n")
task.printArray(arr1, size)
task.distinctSum(arr1, size)
# Get the size
size = arr2.length
print("\n Array elements \n")
task.printArray(arr2, size)
task.distinctSum(arr2, size)
end
main()
Output
Array elements
1 2 7 3 5 7 6 3
Sum of distinct element is : 24
Array elements
0 1 4 2 3 2 4
Sum of distinct element is : 10
/*
Scala Program
Sum of all distinct elements in array
*/
class DistinctElement
{
// Display elements of given array
def printArray(arr: Array[Int], size: Int): Unit = {
var i: Int = 0;
while (i < size)
{
print(" " + arr(i));
i += 1;
}
}
// Find the sum of all distinct element which is exists in given array
def distinctSum(arr: Array[Int], size: Int): Unit = {
// Define auxiliary resultant variables
var sum: Int = 0;
var flag: Int = 0;
// Loop controlling variables
var i: Int = 0;
var head: Int = 0;
var tail: Int = 0;
// Execute loop through by array size
while (i < size)
{
// Decide boundary of inner loop to test repeated element
head = 0;
tail = i - 1;
// Checker which is used to test distinct element
flag = 1;
// Check that [i] elements are used in previously
while (head <= tail && flag == 1)
{
if (arr(head) == arr(i) || arr(tail) == arr(i))
{
// When elements are already taken
flag = 0;
}
else
{
head += 1;
tail -= 1;
}
}
if (flag == 1)
{
// Add distinct element
sum += arr(i);
}
i += 1;
}
print("\n Sum of distinct element is : " + sum + "\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: DistinctElement = new DistinctElement();
// Define array of integer elements
var arr1: Array[Int] = Array(1, 2, 7, 3, 5, 7, 6, 3);
var arr2: Array[Int] = Array(0, 1, 4, 2, 3, 2, 4);
// Get the size
var size: Int = arr1.length;
print(" Array elements \n");
task.printArray(arr1, size);
task.distinctSum(arr1, size);
// Get the size
size = arr2.length;
print("\n Array elements \n");
task.printArray(arr2, size);
task.distinctSum(arr2, size);
}
}
Output
Array elements
1 2 7 3 5 7 6 3
Sum of distinct element is : 24
Array elements
0 1 4 2 3 2 4
Sum of distinct element is : 10
/*
Swift 4 Program
Sum of all distinct elements in array
*/
class DistinctElement
{
// Display elements of given array
func printArray(_ arr: [Int], _ size: Int)
{
var i: Int = 0;
while (i < size)
{
print(" ", arr[i], terminator: "");
i += 1;
}
}
// Find the sum of all distinct element which is exists in given array
func distinctSum(_ arr: [Int], _ size: Int)
{
// Define auxiliary resultant variables
var sum: Int = 0;
var flag: Int = 0;
// Loop controlling variables
var i: Int = 0;
var head: Int = 0;
var tail: Int = 0;
// Execute loop through by array size
while (i < size)
{
// Decide boundary of inner loop to test repeated element
head = 0;
tail = i - 1;
// Checker which is used to test distinct element
flag = 1;
// Check that [i] elements are used in previously
while (head <= tail && flag == 1)
{
if (arr[head] == arr[i] || arr[tail] == arr[i])
{
// When elements are already taken
flag = 0;
}
else
{
head += 1;
tail -= 1;
}
}
if (flag == 1)
{
// Add distinct element
sum += arr[i];
}
i += 1;
}
print("\n Sum of distinct element is : ", sum );
}
}
func main()
{
let task: DistinctElement = DistinctElement();
// Define array of integer elements
let arr1: [Int] = [1, 2, 7, 3, 5, 7, 6, 3];
let arr2: [Int] = [0, 1, 4, 2, 3, 2, 4];
// Get the size
var size: Int = arr1.count;
print(" Array elements ");
task.printArray(arr1, size);
task.distinctSum(arr1, size);
// Get the size
size = arr2.count;
print("\n Array elements ");
task.printArray(arr2, size);
task.distinctSum(arr2, size);
}
main();
Output
Array elements
1 2 7 3 5 7 6 3
Sum of distinct element is : 24
Array elements
0 1 4 2 3 2 4
Sum of distinct element is : 10
/*
Kotlin Program
Sum of all distinct elements in array
*/
class DistinctElement
{
// Display elements of given array
fun printArray(arr: Array < Int > , size: Int): Unit
{
var i: Int = 0;
while (i < size)
{
print(" " + arr[i]);
i += 1;
}
}
// Find the sum of all distinct element which is exists in given array
fun distinctSum(arr: Array < Int > , size: Int): Unit
{
// Define auxiliary resultant variables
var sum: Int = 0;
var flag: Int ;
// Loop controlling variables
var i: Int = 0;
var head: Int ;
var tail: Int ;
// Execute loop through by array size
while (i < size)
{
// Decide boundary of inner loop to test repeated element
head = 0;
tail = i - 1;
// Checker which is used to test distinct element
flag = 1;
// Check that [i] elements are used in previously
while (head <= tail && flag == 1)
{
if (arr[head] == arr[i] || arr[tail] == arr[i])
{
// When elements are already taken
flag = 0;
}
else
{
head += 1;
tail -= 1;
}
}
if (flag == 1)
{
// Add distinct element
sum += arr[i];
}
i += 1;
}
print("\n Sum of distinct element is : " + sum + "\n");
}
}
fun main(args: Array < String > ): Unit
{
var task: DistinctElement = DistinctElement();
// Define array of integer elements
var arr1: Array < Int > = arrayOf(1, 2, 7, 3, 5, 7, 6, 3);
var arr2: Array < Int > = arrayOf(0, 1, 4, 2, 3, 2, 4);
// Get the size
var size: Int = arr1.count();
print(" Array elements \n");
task.printArray(arr1, size);
task.distinctSum(arr1, size);
// Get the size
size = arr2.count();
print("\n Array elements \n");
task.printArray(arr2, size);
task.distinctSum(arr2, size);
}
Output
Array elements
1 2 7 3 5 7 6 3
Sum of distinct element is : 24
Array elements
0 1 4 2 3 2 4
Sum of distinct element is : 10
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment