- 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
How to check if array contains a duplicate number using C#?
Firstly, set an array −
int[] arr = { 87, 55, 23, 87, 45, 23, 98 };
Now declare a dictionary and loop through the array and get the count of all the elements. The value you get from the dictionary displays the occurrence of numbers −
foreach(var count in arr) { if (dict.ContainsKey(count)) dict[count]++; else dict[count] = 1; }
Let us see the complete example −
Example
using System; using System.Collections.Generic; namespace Demo { public class Program { public static void Main(string[] args) { int[] arr = { 87, 55, 23, 87, 45, 23, 98 }; var dict = new Dictionary < int, int > (); foreach(var count in arr) { if (dict.ContainsKey(count)) dict[count]++; else dict[count] = 1; } foreach(var val in dict) Console.WriteLine("{0} occurred {1} times", val.Key, val.Value); } } }
- Related Articles
- C# program to find if an array contains duplicate
- Check if a given array contains duplicate elements within k distance from each in C++
- Check if a string contains a number using Java.
- How to check if a data frame column contains duplicate values in R?
- How to check if an R matrix column contains only duplicate values?
- Check if a Binary Tree contains duplicate subtrees of size 2 or more in C++
- Write a program in Python to check if a series contains duplicate elements or not
- How to check if array contains three consecutive dates in java?
- How to check if an array contains integer values in JavaScript ?
- How to check if a string contains a certain word in C#?
- Java Program to Check if An Array Contains a Given Value
- Golang Program to Check if An Array Contains a Given Value
- Method to check if array element contains a false value in JavaScript?
- PHP 8 – Using str_contains() to check if a string contains a substring
- How to check in C# whether the string array contains a particular work in a string array?

Advertisements