19 Nov 2016

C Program to count duplicate elements in an array

C PROGRAM TO FIND COUNT OF DUPLICATE ELEMENTS IN AN ARRAY


In this post we will see a simple C program to find the count of duplicate elements in an Array.
In below program we will get the count of elements that are duplicated i.e. elements that are occurred more than once in an array.

Example :

Suppose the elements of array are : 10,20,30,10,10

Here the element 10 is repeated 3 times in array, it means it is duplicated 2 more times and remaining elements occurred only once. Hence its output will be 2.

Program Algorithm :
Step 1: Read size of array and store it in n
Step 2: If n (i.e size of array) is greater than MAX, print message and exit program
Step 3: Read elements of array and store in arr[MAX] , here MAX is a global variable whose value
            is 25 in the program below
Step 4: Initialize count = 0, to avoid garbage values
Step 5: Set i = 0
Step 6: Set j = i+1
Step 7: Check for duplicate, if arr[i] = arr[j] then increment count by 1 i.e. count = count + 1
Step 8: Increment j by 1 i.e. j = j + 1 and repeat Step 4-5 till j<n
Step 9: Increment i by 1 i.e. i = i + 1 and repeat Step 3-5 till i<n
Step 10: Print the count and exit program

Program :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include<stdio.h>
#define MAX 25 //maximum size of array is 25

main()
{
  int i,j,n,count,arr[MAX];

  printf(" *** www.ProgrammingPosts.com ***");
  printf("\n >>> C program to count duplicate elements in an array <<<");
  
  //taking size of array as input from user
  printf("\n\n Enter the size of array: ");
  scanf("%d",&n); 
  
  //checking if n value greater than MAX size of array
  if(n > MAX) 
  {
     printf(" Max Array size allowed is %d",MAX);
     getch();
     return; //terminates program execution   
  }
  
  printf("\n Enter the %d elements of array: \n",n);
  
  //Reading the elements of array using for loop
  for(i=0;i<n;i++)
  {
    printf("\n arr[%d]:",i);
    scanf("%d",&arr[i]);    
  }
   
   //initializing the count to 0, to avoid garbage value
   count =0; 

   //Counting the duplicate elements in Array
  for(i=0; i<n; i++)
  {
     for(j=i+1; j<n; j++)
     {
       /* If duplicate found then increment count by 1 */
       if(arr[i]==arr[j])
       {
         count++;
         break;
       }
     }
   }
    
   printf("\n Count of duplicate elements in an array : %d\n",count);
   getch();
   return 0;
}

Sample output :


Sample output text :
 *** www.ProgrammingPosts.com ***
 >>> C program to count duplicate elements in an array <<<
 Enter the size of array: 5
 Enter the 5 elements of array:
 arr[0]:10
 arr[1]:20
 arr[2]:30
 arr[3]:10
 arr[4]:20
 Count of duplicate elements in an array : 2

7 Nov 2016

Get Financial Year of a given Date in C#.Net / VB.Net

In this post we will see, generating a Financial Year string based on the given date.
I have given code in C#.Net and VB.Net.

C#.Net Method to Get Financial Year of a given Date :


    /// <summary>
    /// Method to Get Financial Year of a given Date
    /// </summary>
    /// <param name="curDate"></param>
    /// <returns></returns>
    public string GetFinancialYear(DateTime curDate)
    {
        int CurrentYear = curDate.Year;
        int PreviousYear = (curDate.Year - 1);
        int NextYear = (curDate.Year + 1);

        string PreYear = PreviousYear.ToString();
        string NexYear = NextYear.ToString();
        string CurYear = CurrentYear.ToString();

        string FinYear = null;

        if (curDate.Month > 3)
        {
            FinYear = CurYear + "-" + NexYear;
        }
        else
        {
            FinYear = PreYear + "-" + CurYear;
        }
        return FinYear;
    }


VB.Net Method to Get Financial Year of a given Date :

    ''' <summary>
    ''' Method to Get Financial Year of a given Date
    ''' </summary>
    ''' <param name="curDate"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetFinancialYear(ByVal curDate As DateTime) As String

        Dim CurrentYear As Integer = curDate.Year
        Dim PreviousYear As Integer = curDate.Year - 1
        Dim NextYear As Integer = curDate.Year + 1

        Dim PreYear As String = PreviousYear.ToString()
        Dim NexYear As String = NextYear.ToString()
        Dim CurYear As String = CurrentYear.ToString()

        Dim FinYear As String = Nothing

        If (curDate.Month > 3) Then
            FinYear = CurYear + "-" + NexYear
        Else
            FinYear = PreYear + "-" + CurYear

        End If
        Return FinYear

    End Function

23 Sep 2016

C# Program to count particular digit in a given number


C# PROGRAM TO FIND THE OCCURRENCE OF A DIGIT IN A NUMBER

In the previous posts, i have given C#.Net code to count the occurrences of a character in a string

This program is to count the occurrences of a digit in a given number.

I have written 3 different methods for the same thing.

8 May 2016

Check Uncheck All Checkboxes in Html Table Using JQuery

In this blog post, we will see a small example to check or uncheck all checkboxes in a html table using jquery. You can implement the same code in Asp.net gridview.

To make this work, first you need to use jquery in your web page.

There are many articles on web providing code samples to check or uncheck all checkboxes.
But here we will also see how a header checkbox acts indeterminate when few of the checkboxes are checked/unchecked.

18 Oct 2015

DataTable to Dictionary Conversion in C#.Net

In this post, here is the c# code to convert DataTable to Dictionary.
while working with DataSet or DataTable we may need key value pair to get Value by Id approach.
So you may need to convert your DataTable to key value type i.e, Dictionary.

you can also use Dictionary for data binding of controls like Dropdown list, Radio button list etc.

Here we will see a sample console program where we convert DataTable to Dictionary and print the Dictionary.
I have written 2 overrides for a method DataTabelToDictionary(). The Method code is given below: