

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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 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 Questions & Answers
- How to check if array contains a duplicate number using C#?
- C# program to find all duplicate elements in an integer array
- Find if an array contains a string with one mismatch in C++
- Java Program to Check if An Array Contains a Given Value
- Java Program to Check if An Array Contains the Given Value
- How to check if an R matrix column contains only duplicate values?
- Contains Duplicate III in C++
- Contains Duplicate II in C++
- C Program to delete the duplicate elements in an array
- Check if a given array contains duplicate elements within k distance from each in C++
- Find if an expression has duplicate parenthesis or not in C++
- C++ Program to find an array satisfying an condition
- Write a C Program to delete the duplicate numbers in an array
- How to check if an array contains integer values in JavaScript ?
- Write a program in Python to check if a series contains duplicate elements or not
Advertisements