- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 if an array contains duplicate
Set an array −
int[] arr = { 89, 12, 56, 89, };
Now, create a new Dictionary −
var d = new Dictionary < int, int > ();
Using the dictionary method ContainsKey(), find the duplicate elements in the array −
foreach(var res in arr) { if (d.ContainsKey(res)) d[res]++; else d[res] = 1; }
Here is the complete code −
Example
using System; using System.Collections.Generic; namespace Demo { public class Program { public static void Main(string[] args) { int[] arr = { 89, 12, 56, 89, }; 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
89 occurred 2 times 12 occurred 1 times 56 occurred 1 times
- Related Articles
- How to check if array contains a duplicate number using C#?
- C# program to find all duplicate elements in an integer array
- C Program to delete the duplicate elements in an array
- Java Program to Check if An Array Contains a Given Value
- Java Program to Check if An Array Contains the Given Value
- Golang Program to Check if An Array Contains a Given Value
- Find if an array contains a string with one mismatch in C++
- How to check if an R matrix column contains only duplicate values?
- Check if a given array contains duplicate elements within k distance from each in C++
- Write a C Program to delete the duplicate numbers in an array
- Swift Program to Remove Duplicate Elements From an Array
- Golang Program To Remove Duplicate Elements From An Array
- Contains Duplicate II in C++
- Contains Duplicate III in C++
- C++ program to find array after removal of left occurrences of duplicate elements

Advertisements