TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Find the Count of Repetitive Number using Dictionary
Kaushik S
Apr 27
2016
Code
885
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
namespace
CSharpGitHubPgm
{
class
Program
{
static
void
Main(
string
[] args)
{
int
[] arr = { 1, 2, 3, 1, 5, 2, 1 };
Dictionary<
int
,
int
> storeRepeatedNumber =
new
Dictionary<
int
,
int
>();
Console.WriteLine(
"To find the repeatative element in the array"
);
for
(
int
i = 0; i < arr.Length; i++)
{
if
(
storeRepeatedNumber
.ContainsKey(arr[i]))
{
int
previousValue =
storeRepeatedNumber
[arr[i]];
storeRepeatedNumber
[arr[i]] = previousValue + 1;
}
else
{
storeRepeatedNumber
.Add(arr[i], 1);
}
}
foreach
(KeyValuePair<
int
,
int
> item
in
storeRepeatedNumber
)
{
Console.WriteLine(
"{0} Occurs {1} times"
, item.Key,item.Value);
}
Console.ReadLine();
}
}
}
C#