forked from RyanFehr/HackerRank
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSolution.cs
52 lines (46 loc) · 1.72 KB
/
Solution.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
51
52
/*
Problem: https://www.hackerrank.com/challenges/sock-merchant/problem
C# Language Version: 6.0
.Net Framework Version: 4.5.2
Thoughts :
1. Take the input array containing colors (number coded) of n socks in the pile.
2. Initialize a counter named "pairsFound" with 0.
3. Initialize a dictionary named "sockColorHash" which will store color coded number of a sock as key.
3. Start iterating the socks pile array.
4.1 Let the current socks color be k.
4.2 If k key is not present in sockColorHash dictionary then add it into the dictionary with value 1.
4.3 If k key is present in sockColorHash dictionary then remove it into the dictionary and increment
pairsFound counter by 1.
5. Print pairsFound.
Time Complexity: O(n) //we have to iterate the socks pile array only once in a loop.
Space Complexity: O(n) //only one dynamically allocated dictionary dependent on input array size.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {
static int sockMerchant(int[] socksPile)
{
var pairsFound = 0;
var sockColorHash = new Dictionary<int, int>();
foreach (var sock in socksPile)
{
if (sockColorHash.ContainsKey(sock))
{
pairsFound++;
sockColorHash.Remove(sock);
}
else
sockColorHash.Add(sock, 1);
}
return pairsFound;
}
static void Main(String[] args) {
Console.ReadLine();
var ar_temp = Console.ReadLine().Split(' ');
var ar = Array.ConvertAll(ar_temp, Int32.Parse);
var result = sockMerchant(ar);
Console.WriteLine(result);
}
}