Find element occurring more than half in an unsorted array
Problem
To find an element occurring more than half in an unsorted array.
Success Use Case
- An array will be defined in which we have one element occurring more than half.
- When user runs the program it should print the element along with occurrence count.
Failure Use Case
- An array will be defined in which we have no element occurring more than half.
- When user runs the program it should print the element along with occurrence count.
Implementation Notes
- We will make an array from a comma separated string that has numbers in no particular order so that we can meet the requirement of having an unsorted array.
- We will split the string and make an array out of it.
- Looking at the problem we may have to maintain a data structure that can be used to keep track of the count and it should be easier to access. At this point I am thinking to use a dictionary because that will be comparatively more suitable for this problem. Dictionary will be of key Integer type and value Integer type.
- Looking at the count of string array we will divide it by half to define the half occurrence count.
- We will loop thorough the array:
- If element is not present in the list we will add it as key with default value as 1.
- If element is already present then update the value to the next count and also check if count value is greater than the half occurrence count in Step 4. If so we will set a flag that we have found element of choice.
- After we exit from the loop we will check the highest occurrence count in the dictionary and store the key for that element.
- No its just the matter of looking at the flag and printing the relevant message.
- We should be done.
Code
MoreThanFiftyPercentRepeatingNumberFromUnsortedArray.cs
static class MoreThanFiftyPercentRepeatingNumberFromUnsortedArray
{
public static void AlgorithmImplementation(string inputString)
{
//getStringInput = "1,3,4,5,6,8,9,9,7,5,4,6,4,6,4,2,5,3,5,5,6,6,6,6";
var found = false;
var inputArray = inputString.Split(',');
var halfOccuranceCount = (inputArray.Length + 1) / 2;
IDictionary<int, int> occuranceDictionary = new Dictionary<int, int>();
for (var input=0; input < inputArray.Length;input++)
{
var intVaue = Convert.ToInt32(inputArray[input]);
if(occuranceDictionary.ContainsKey(intVaue))
{
occuranceDictionary[intVaue]++;
if(occuranceDictionary[intVaue]>halfOccuranceCount)
{
found = true;
}
}
else
{
occuranceDictionary.Add(intVaue, 1);
}
}
var highestOccuranceKey = occuranceDictionary.Aggregate((l, x) => l.Value > x.Value ? l : x).Key;
if (!found)
{
Console.WriteLine("Use Case Failure... \r\n Total Count({0}) \r\n Success Criteria Count({1}) \r\n Next Highest Occurring Value({2}) \r\n Occurrence Count({3}) ", (inputArray.Length + 1), halfOccuranceCount, highestOccuranceKey, occuranceDictionary[highestOccuranceKey]);
}
else
{
Console.WriteLine("Use Case Success!!! \r\n Total Count({0}) \r\n Success Criteria Count({1}) \r\n Highest Occurring Value({2}) \r\n Occurrence Count({3})", (inputArray.Length + 1),halfOccuranceCount, highestOccuranceKey, occuranceDictionary[highestOccuranceKey]);
}
}
}
Program.cs
static void Main(string[] args)
{
Algorithm_MoreThanFiftyPercentRepeatingNumberFromUnsortedArray();
Console.ReadLine();
}
static void Algorithm_MoreThanFiftyPercentRepeatingNumberFromUnsortedArray()
{
string successString = "9,8,5,6,3,4,7,9,9,9,9,9,3,7,9,9,9,9,9,9,9,9,9,9,9,9";
string failureString = "1,3,4,5,6,8,9,9,7,5,4,6,4,6,4,2,5,3,5,5,6,6,6,6";
var keepRunning = true;
while (keepRunning)
{
Console.WriteLine("Enter 1 for Success Case.");
Console.WriteLine("Enter 2 for Success Failure.");
Console.WriteLine("Else to Exit.\r\n");
Console.Write("\r\ninput>");
var x = Console.ReadLine();
switch(x)
{
case "1":
Console.WriteLine("Input String: {0}", successString);
MoreThanFiftyPercentRepeatingNumberFromUnsortedArray.AlgorithmImplementation(successString);
Console.WriteLine("**********************************");
break;
case "2":
Console.WriteLine("Input String: {0}", failureString);
MoreThanFiftyPercentRepeatingNumberFromUnsortedArray.AlgorithmImplementation(failureString);
Console.WriteLine("**********************************");
break;
default:
Console.WriteLine("Exiting...");
keepRunning = false;
break;
}
}
}
Comments
Post a Comment