1. Find the two repeating elements in a given array
Case : 1
class Program
{
static void Main(string[] args)
{
//Find the two repeating elements in a given array
int[] array = { 4, 2, 4, 5, 2, 2, 3, 1 ,4};
Dictionary<int, int> duplicateNumbers = new Dictionary<int, int>();
int count = 1;
for (int i = 0; i < array.Length; i++)
{
count = 1;
if (!duplicateNumbers.ContainsKey(array[i]))
{
for (int j = i; j < array.Length - 1; j++)
{
if (array[i] == array[j + 1])
{
count++;
}
}
if (count > 1)
{
duplicateNumbers.Add(array[i], count);
}
}
}
foreach (var num in duplicateNumbers)
{
Console.WriteLine("Duplicate numbers, Number-{0}, Occurrence- {1}", num.Key, num.Value);
}
Console.ReadLine();
}
}
Output:
Duplicate numbers, Number-4, Occurrence- 3
Duplicate numbers, Number-2, Occurrence- 3
Case : 2
class Program
{
static void Main(string[] args)
{
//Find the two repeating elements in a given array
int[] arr = { 4, 2, 4, 5, 2, 3, 1 };
for (int i = 0; i < arr.Length; i++)
{
for (int J = i+1; J < arr.Length; J++)
{
if (arr[i]==arr[J])
{
Console.WriteLine(arr[i]);
}
}
}
Console.ReadLine();
}
}
Output:-
4
4
2
Case : 3
LINQ through Find the two repeating elements in a given array
class Program
{
static void Main(string[] args)
{
//Find the two repeating elements in a given array
int[] arr = { 4, 2, 4, 5, 2, 2, 3, 1 ,4};
var data = arr.GroupBy(x => x).Select(x => new { key = x.Key, val = x.Count() });
foreach (var item in data)
{
if (item.val > 1)
{
Console.WriteLine("Duplicate value : {0}",item.key);
Console.WriteLine("MaxCount : {0}", item.val);
}
}
Console.ReadLine();
}
}
Output :
Duplicate value : 4
MaxCount : 3
Duplicate value : 2
MaxCount : 3
Case : 1
class Program
{
static void Main(string[] args)
{
//Find the two repeating elements in a given array
int[] array = { 4, 2, 4, 5, 2, 2, 3, 1 ,4};
Dictionary<int, int> duplicateNumbers = new Dictionary<int, int>();
int count = 1;
for (int i = 0; i < array.Length; i++)
{
count = 1;
if (!duplicateNumbers.ContainsKey(array[i]))
{
for (int j = i; j < array.Length - 1; j++)
{
if (array[i] == array[j + 1])
{
count++;
}
}
if (count > 1)
{
duplicateNumbers.Add(array[i], count);
}
}
}
foreach (var num in duplicateNumbers)
{
Console.WriteLine("Duplicate numbers, Number-{0}, Occurrence- {1}", num.Key, num.Value);
}
Console.ReadLine();
}
}
Output:
Duplicate numbers, Number-4, Occurrence- 3
Duplicate numbers, Number-2, Occurrence- 3
Case : 2
class Program
{
static void Main(string[] args)
{
//Find the two repeating elements in a given array
int[] arr = { 4, 2, 4, 5, 2, 3, 1 };
for (int i = 0; i < arr.Length; i++)
{
for (int J = i+1; J < arr.Length; J++)
{
if (arr[i]==arr[J])
{
Console.WriteLine(arr[i]);
}
}
}
Console.ReadLine();
}
}
Output:-
4
4
2
Case : 3
LINQ through Find the two repeating elements in a given array
class Program
{
static void Main(string[] args)
{
//Find the two repeating elements in a given array
int[] arr = { 4, 2, 4, 5, 2, 2, 3, 1 ,4};
var data = arr.GroupBy(x => x).Select(x => new { key = x.Key, val = x.Count() });
foreach (var item in data)
{
if (item.val > 1)
{
Console.WriteLine("Duplicate value : {0}",item.key);
Console.WriteLine("MaxCount : {0}", item.val);
}
}
Console.ReadLine();
}
}
Output :
Duplicate value : 4
MaxCount : 3
Duplicate value : 2
MaxCount : 3