-
Notifications
You must be signed in to change notification settings - Fork 0
/
NutsBolts_Problem.cs
50 lines (47 loc) · 1.08 KB
/
NutsBolts_Problem.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
* Algorithm to find the common element in given three sorted arrays
*/
using System;
using System.Collections.Generic;
namespace ConsoleApp56
{
class Program
{
public static void Main()
{
char[] nuts = { '$', '%', '&', 'x', '@' };
char[] bolts = { '%', '@', 'x', '$', '&' };
Match(nuts, bolts);
}
private static void Match(char[] nuts, char[] bolts)
{
Dictionary<char, int> dict = new Dictionary<char, int>();
for(int i=0;i<nuts.Length;i++)
{
dict.Add(nuts[i], i);
}
for(int i=0;i<bolts.Length;i++)
{
char bolt = bolts[i];
if(dict.ContainsKey(bolt))
{
nuts[i] = bolts[i];
}
else
{
Console.WriteLine("for bolt " + bolt + " no nut is present ");
return;
}
}
Console.WriteLine("Matched nuts and bolts are: ");
foreach(var t in nuts)
{
Console.Write( "Current state : " +t);
}
foreach (var p in bolts)
{
Console.Write("\n After matching state : " +p);
}
}
}
}