-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLesson2_Arrays_OddOccurrencesInArray.cs
123 lines (88 loc) · 3.9 KB
/
Lesson2_Arrays_OddOccurrencesInArray.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
namespace Lesson2_Arrays_OddOccurrencesInArray
{
class Solution {
public void Test()
{
if(true)
{
var Solution = new Lesson2_Arrays_OddOccurrencesInArray.Solution();
int x;
String passFail = "";
x=Solution.solution(new int[] { 1 } );
passFail = x == 1 ? "PASS" : "FAILED";
Console.WriteLine($"{ passFail}");
Console.WriteLine($" ============================================ Next ============================================ ");
x=Solution.solution(new int[] { 1,2,3 ,1,2} );
passFail = x == 3 ? "PASS" : "FAILED";
Console.WriteLine($"{ passFail}");
Console.WriteLine($" ============================================ Next ============================================ ");
x=Solution.solution(new int[] { 1 ,1,2,1,2,3,1} );
passFail = x == 3 ? "PASS" : "FAILED";
Console.WriteLine($"{ passFail}");
Console.WriteLine($" ============================================ Next ============================================ ");
x=Solution.solution(new int[] { 1 ,2,3,4,5,5,4,3,2,1,6,7,8,9,0,0,9,8,7,6,5} );
passFail = x == 5 ? "PASS" : "FAILED";
Console.WriteLine($"{ passFail}");
Console.WriteLine($" ============================================ Next ============================================ ");
x=Solution.solution(new int[] { 1 ,2,3,4,5,4,3,2,1,6,7,8,9,0,0,9,8,7,6,1,2,3,4,4,3,2,1,5,55,55,99,1,1,2,2,3,2,3,2} );
passFail = x == 99 ? "PASS" : "FAILED";
Console.WriteLine($"{ passFail}");
Console.WriteLine($" ============================================ Next ============================================ ");
x=Solution.solution(new int[] { 1 ,2,1} );
passFail = x == 2 ? "PASS" : "FAILED";
Console.WriteLine($"{ passFail}");
Console.WriteLine($" ============================================ Next ============================================ ");
}
}
public void log(string l)
{
//Console.WriteLine(l);
}
public int solution(int[] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
log($"inside solution.solution input param [{string.Join(" , ", A)}]");
// iterate over entire list,
// for each item, add counter to dictionary <int Key, int count>
// loop over dictionary until we find an odd entry.
var lenA = A.Length;
log($"lenA [{lenA}]");
var dict = new Dictionary<int, int>();
foreach(int i in A)
{
if(dict.ContainsKey(i))
{
log($"HAS KEY [{i}]");
dict[i]++;
}
else
{
// Console.WriteLine($"CREATING KEY [{i}]");
dict.Add(i, 1);
}
}
// iterate over dict to find odd entry
foreach(var k in dict)
{
log($" KEY [{k}] VALUE [{dict[k.Key]}] mod [{ k.Value % 2 }]");
}
// iterate over dict to find odd entry
foreach(var k in dict)
{
if(k.Value % 2 == 1)
{
log($"RESULT [{k.Key}]]");
return k.Key;
}
}
// something went wrong .. shouldn't get here if initial dataset was right.
log($"RESULT 0");
return 0;
}
}
}