Answered explain "Mark duplicates" code?

sock1992

Well-known member
Joined
May 20, 2020
Messages
107
Programming Experience
Beginner
Hi, the program below counts a total number of duplicate elements in an array. The code under "Mark the elements are duplicates" is the part i'm having trouble with.

C#:
    public class Exercise5
    {
        public static void Main()
        {
            int[] arr1 = new int[100];
            int[] arr2 = new int[100];
            int[] arr3 = new int[100];
            int s1, s2, mm = 1, ctr = 0;
            int i, j;
            Console.Write("\n\nCount total number of duplicate elements in an array:\n");
            Console.Write("---------------------------------------------------------\n");

            Console.Write("Input the number of elements to be stored in the array :");
            s1 = Convert.ToInt32(Console.ReadLine());

            Console.Write("Input {0} elements in the array :\n", s1);
            for (i = 0; i < s1; i++)
            {
                Console.Write("element - {0} : ", i);
                arr1[i] = Convert.ToInt32(Console.ReadLine());
            }
            /*----------------- copy in other array ------------------------------------*/
            for (i = 0; i < s1; i++)
            {
                arr2[i] = arr1[i];
                arr3[i] = 0;
            }
            /*------------------- mark the elements are duplicate -------------------------*/
            for (i = 0; i < s1; i++)
            {
                for (j = 0; j < s1; j++)
                {
                    if (arr1[i] == arr2[j])
                    {
                        arr3[j] = mm;
                        mm++;
                    }
                }
                mm = 1;
            }
            /*--------------- Prints the array ------------------------------------*/
            for (i = 0; i < s1; i++)
            {
                if (arr3[i] == 2) { ctr++; }
            }
            Console.Write("The number of duplicate elements is: {0} \n", ctr);

            Console.Write("\n\n");
        }
    }
 
Last edited by a moderator:
Line 43 won't even compile. Or for that matter neither will line #25. Are you sure you typed your program in correctly?
 
I added the code in your link to your opening topic.

In your original code, you have a [B] tag under :
/*------------------- mark the elements are duplicate -------------------------*/

Notice how there are no such tags in the link you provided. Are you using visual studio as your IDE?
 
I added the code in your link to your opening topic.

In your original code, you have a [B] tag under :
/*------------------- mark the elements are duplicate -------------------------*/

Notice how there are no such tags in the link you provided. Are you using visual studio as your IDE?

yes im using visual studio
 
Rid your self of the bold tags here :
[B]
C#:
for(i=0;i<s1; i++)
        {
        for(j=0;j<s1;j++)
            {
                if(arr1==arr2[j])
                {
                arr3[j]=mm;

                mm++;
                }
            }
            mm=1;
        }
[/B]
Tholse B brackets don't belong there.
 
The best way to understand what the code is doing is to try simulating the code with a pen and paper. Don't just run it on the computer. If you have past debugging experience, then try stepping through the code with the debugger, but in my experience beginner learns get more insight when they first do things with pen and paper.
 
It won't compile with bold tags, so that rules out debugging xD

Here's the code OP posted originally. You can see in the snipped, he has bold tags which need removing. :
C#:
/*------------------- mark the elements are duplicate -------------------------*/
 [B]   for(i=0;i<s1; i++)
        {
        for(j=0;j<s1;j++)
            {
                if(arr1==arr2[j])
                {
                arr3[j]=mm;
                mm++;
                }
            }
            mm=1;
        }[/B]
/*--------------- Prints the array ------------------------------------*/
 
@soggla1992 did you intend to post that code with bold tags?

If not, make sure that not in your code editor.

And if you did intend to post those bold tags, I'll just point out that you don't need to highlight things for us. It just misleads us into thinking you made a mistake while copy/pasting.

If that's not your issue, then you need to start explaining the problem you have.
What your expectations are.
And what is actually happening.
 
Back
Top Bottom