A PHP Error was encountered

Severity: 8192

Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

Filename: libraries/Filtered_db.php

Line Number: 23

Write C# program to print all unique element in an array
Q:

Write C# program to print all unique element in an array

-1

Write C# program to print all unique element in an array

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

I have used Visual Studio 2012 for debugging purpose. But you can use any version of visul studio as per your availability.

using System;
class Program
{
    static void Main()
    {
        int[] arr = new int[100];;
        int i, j, k, size, isUnique;
 
        //Reads size of the array
        Console.WriteLine("Enter size of the array: ");
        size = Convert.ToInt32(Console.ReadLine());
        //Reads elements in array
        Console.WriteLine("Enter elements in the array: ");              
        for(i=0; i<size; i++)
        {
            arr[i] = Convert.ToInt32(Console.ReadLine());            
        }
 
        //Removing all duplicate elements from the array
 
         for(i=0; i<size; i++)
        {
            // Assuming  cuurent element is unique */
            isUnique = 1;
 
            for(j=i+1; j<size; j++)
            {
 
                //If any duplicate element is found
 
                if(arr[i]==arr[j])
                {
                    // Removing duplicate element
                    for(k=j; k<size-1; k++)
                    {
                        arr[k] = arr[k+1];
                    }
 
                    size--;
                    j--;
                    isUnique = 0;
                }
           }
 
            /*
        If array element is not unique
        then also remove the current element
         */
            if (isUnique != 1)
            {
                for (j = i; j < size - 1; j++)
                {
                    arr[j] = arr[j + 1];
                }
 
                size--;
                i--;
            }
        }
 
         //Printing all unique elements in array
         Console.WriteLine("All unique elements in the array are: ");
         for (i = 0; i < size; i++)
         {
             Console.WriteLine(arr[i] + "\t");
         }
        Console.ReadLine();
    }
 
}

Result:

Enter size of the array: 

10

Enter elements in the array: 

1

2

3

4

5

1

2

3

10

20

All unique elements in the array are: 

4

5

10

20

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now