Skip to content
Loading
How to print a unique elements of an Array?
  • Manikandan Murugesan
    Refer the below code to print unique elements in array:
    1. using System;    
    2. public class Exercise6  
    3. {    
    4.     public static void Main()   
    5. {  
    6.     int  n,ctr=0;  
    7.     int[] arr1 = new int[100];  
    8.     int i, j, k;   
    9.       
    10.       
    11.        Console.Write("\n\nPrint all unique elements of an array:\n");  
    12.        Console.Write("------------------------------------------\n");     
    13.   
    14.        Console.Write("Input the number of elements to be stored in the array :");  
    15.        n = Convert.ToInt32(Console.ReadLine());          
    16.      
    17.        Console.Write("Input {0} elements in the array :\n",n);  
    18.        for(i=0;i
    19.             {  
    20.           Console.Write("element - {0} : ",i);  
    21.           arr1[i] = Convert.ToInt32(Console.ReadLine());            
    22.         }  
    23.   
    24.     /*Checking duplicate elements in the array */  
    25.     Console.Write("\nThe unique elements found in the array are : \n");  
    26.     for(i=0; i
    27.     {  
    28.         ctr=0;  
    29.           
    30.         /*Check duplicate bifore the current position and 
    31.         increase counter by 1 if found.*/  
    32.         for(j=0; j
    33.         {  
    34.             /*Increment the counter when the seaarch value is duplicate.*/  
    35.             if(arr1[i]==arr1[j])  
    36.             {  
    37.                 ctr++;  
    38.             }  
    39.         }  
    40.         /*Check duplicate after the current position and 
    41.           increase counter by 1 if found.*/  
    42.        for(k=i+1; k
    43.         {  
    44.             /*Increment the counter when the seaarch value is duplicate.*/  
    45.             if(arr1[i]==arr1[k])  
    46.             {  
    47.                 ctr++;  
    48.             }  
    49.         }  
    50.         /*Print the value of the current position of the array as unique value  
    51.         when counter remain contains its initial value.*/  
    52.        if(ctr==0)  
    53.         {  
    54.           Console.Write("{0} ",arr1[i]);  
    55.         }  
    56.     }  
    57.        Console.Write("\n\n");  
    58.   }  

    0
  • Naveen Arumugam
    can you explain with C# Program using foreach statement to print an unique array.Thank You...
    0
  • Sundaram Subramanian
    #include
    #include
    using namespace std;
    void printDistinct(int arr[], int n)
    {
    // Pick all elements one by one
    for (int i=0; i
    {
    // Check if the picked element is already printed
    int j;
    for (j=0; j
    if (arr[i] == arr[j])
    break;
    // If not printed earlier, then print it
    if (i == j)
    cout << arr[i] << " ";
    }
    }
    // Driver program to test above function
    int main()
    {
    int arr[] = {6, 10, 5, 4, 9, 120, 4, 6, 10};
    int n = sizeof(arr)/sizeof(arr[0]);
    printDistinct(arr, n);
    return 0;
    }
    Run on IDE
    0