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
Calculate the positive min difference between 2 values in a array using Dictionary
Kaushik S
Apr 27
2016
Code
1.4
k
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,9,4,3};
Dictionary<
string
,
int
> storeDiffbetweenNumbers =
new
Dictionary<
string
,
int
>();
Console.WriteLine(
"To find the minimum difference between the array"
);
for
(
int
i = 0; i < arr.Length; i++)
{
for
(
int
j = i+1; j < arr.Length; j++)
{
int
diff = arr[i] - arr[j];
string
key = Convert.ToString(arr[i] +
"-"
+ arr[j]);
storeDiffbetweenNumbers.Add(key, diff);
}
}
var t = storeDiffbetweenNumbers.Where(x => x.Value > 0).Select(x => x).Min(x=>x.Value);
Console.WriteLine(t);
Console.ReadLine();
}
}
}
C#