C# Program to find duplicate elements in the array


In this program, You will learn how to find duplicate elements in the array in C#.


List is: 11 12 11 13 14 15 13

Duplicate elements are: 11 13

Example: How to find duplicate elements in the array in C#

using System;
public class Program
{
	public static void Main(string[] args)
	{
		int i, j, v = 1;
		int[] arr = new int[5];

		Console.Write("Enter Five numbers:");
		for (i = 0; i < arr.Length; i++)
		{
			arr[i] = Convert.ToInt32(Console.ReadLine());
		}

		Console.Write("duplicate elements:");
		for (i = 0; i < arr.Length; i++)
		{
			for (j = i + 1; j < arr.Length; j++)
			{
				if (arr[i] == arr[j])
				{
					if (v == 1 && arr[j] != '\0')
					{
						Console.Write(arr[i] + " ");
					}
					arr[j] = '\0';
					v++;
				}
			}
			v = 1;
		}
	}
}

Output:

Enter Five numbers:10
20
10
20
40
duplicate elements:10 20