How to calculate the sum & product of dis-similar numbers of an array[10].
i do have a code but it stop working after a certain limit, so please help.....

Dani AI

Generated

Clarification and diagnosis: in this thread “dis‑similar” is best interpreted as the values that occur exactly once in the array (not “distinct set” or “one‑of‑a‑kind positions”). ’s code reads values with Convert.ToInt16 and uses in‑place sentinel values; those choices explain the symptom “stops working after a certain limit” — Int16 throws or truncates beyond ±32767, and overwriting duplicates with 1 corrupts real 1 values. ’s nested‑loop count works functionally but is O(n²) and gets slow for larger arrays.

Recommended, robust approach (O(n) time, O(n) extra memory): make a single pass to build a frequency map (hash/dictionary), then iterate the map to collect values whose count == 1. Sum those values and compute their product. In languages with bounded integer types, use a larger integer type or arbitrary‑precision (C#: long or BigInteger; Python has unlimited ints). Decide what to return for “no unique elements”: mathematically the empty product is 1, but many practical programs return 0 by convention — pick the convention that matches the problem.

Example in Python:

from collections import Counter
from math import prod

def sum_and_prod_unique(arr):
    counts = Counter(arr)
    uniques = [x for x, n in counts.items() if n == 1]
    total = sum(uniques)
    product = prod(uniques) if uniques else 1   # change to 0 if that convention is required
    return total, product

Troubleshooting notes and cautions:

  • Avoid small fixed input parsers like Convert.ToInt16; use int.Parse/Convert.ToInt32 (C#) or Python int() to accept the usual integer range.
  • Don’t use a data value (e.g., 1 or 0) as a marker for “seen” — use a separate boolean map or the frequency map.
  • For languages with overflow, use checked arithmetic or arbitrary precision when products can grow large.
  • For very large inputs prefer the Counter/dictionary method to the nested loops for predictable performance.

Recommended Answers

All 4 Replies

Let's see what you have

Let's see what you have

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int i, j, k, sum, c;
            

            sum = 0;
            int[] m = new int[10];
            for (k = 0; k < 10; k++)
            {
                Console.Write("Enter Value: ");
                m[k] = Convert.ToInt16(Console.ReadLine());
            }

            for (i = 0; i < 10; i++)
            {

                c = 0;

                for (j = i + 1; j < 10; j++)
                {

                    if (m[i] == m[j] && c==0)
                    {
                        sum = sum + m[i];
                        sum = sum + m[j];
                        m[j] = 0;

                        c++;

                    }
                    else if (m[i] == m[j])
                    {
                        
                        sum = sum + m[j];
                        m[j] = 0;



                    }



                }



            }
            Console.WriteLine("sum of similar numbers : " + sum);

            Console.ReadLine();
        }
    }
}

// sum of dissimilar numbers can simply be found by subtracting the sum of similar numbers from the total sum.....

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            int i, j, k, p;


            p = 1;
            int[] m = new int[4];
            for (k = 0; k < 4; k++)
            {
                Console.Write("Enter Value: ");
                m[k] = Convert.ToInt16(Console.ReadLine());
            }

            for (i = 0; i < 4; i++)
            {


                for (j = i + 1; j < 4; j++)
                {
                    if (m[i] == m[j])
                    {
                        m[j] = 1;
                        if (j == 3)
                        {
                            m[i] = 1;
                        }
                    }


                }








            }
            for (k = 0; k < 4; k++)
            {
                p = p * m[k];
            }
            if (p == 1)
            {
                Console.WriteLine("Product of dis-similar numbers :  0");

            }
            else
            {
                Console.WriteLine("sum of similar numbers : " + p);
            }
            Console.ReadLine();
        }
    }
}

Let's see what you have

using System;

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr = { 3, 5, 7, 1, 3, 8, 7, 1, 1, 10, 15, 32, 3 };

            int sum = 0;
            int mul = 1;
            int count = 0;

            for (int k = 0; k < arr.Length; k++ )
            {
                for (int i = 0; i < arr.Length; i++)
                    if (arr[k] == arr[i])
                        count++;

                if (count == 1)
                {
                    sum += arr[k];
                    mul *= arr[k];
                }

                count = 0;
            }

            Console.WriteLine("Sum = : {0}\nMul = : {1}", sum, mul);
            Console.ReadKey();
        }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.