Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C# program to find all duplicate elements in an integer array
Firstly, set the array with duplicate elements.
int[] arr = {
24,
10,
56,
32,
10,
43,
88,
32
};
Now declare a Dictionary and loop through the array to get the repeated elements.
var d = new Dictionary < int, int > ();
foreach(var res in arr) {
if (d.ContainsKey(res))
d[res]++;
else
d[res] = 1;
}
Example
using System;
using System.Collections.Generic;
namespace Demo {
public class Program {
public static void Main(string[] args) {
int[] arr = {
24,
10,
56,
32,
10,
43,
88,
32
};
var d = new Dictionary < int, int > ();
foreach(var res in arr) {
if (d.ContainsKey(res))
d[res]++;
else
d[res] = 1;
}
foreach(var val in d)
Console.WriteLine("{0} occurred {1} times", val.Key, val.Value);
}
}
}
Output
24 occurred 1 times 10 occurred 2 times 56 occurred 1 times 32 occurred 2 times 43 occurred 1 times 88 occurred 1 times
Advertisements