I have a program that reads an ASCII file and outputs a text file with each character and the number of times it appears in the file. A sorted dictionary was used in this program but I was wondering if you could instead use an array to store the characters? The code is below:
using System;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.IO;
using System.Text;
using System.Reflection;
namespace ASCII
{
class CharacterFrequency
{
private char ch;
private int frequency;
public char Ch
{
get { return ch; }
set { ch = value; }
}
public int Frequency
{
get { return frequency; }
set { frequency = value; }
}
}
class Program
{
public string InputFileName = "";
public string OutputFileName = "output.txt";
public string FilePath = "";
public static SortedDictionary<char, ulong> Count(string stringToCount)
{
SortedDictionary<char, ulong> characterCount = new SortedDictionary<char, ulong>();
foreach (var character in stringToCount)
{
if (!characterCount.ContainsKey(character)) // added character to dictionary if only character is absent in charactercount
{
characterCount.Add(character, 1);
}
else
{
characterCount[character]++; // increemetned count
}
}
return characterCount;
}
static void Main(string[] args)
{
var filePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var inputFileName = Path.Combine(filePath, args[0]);
var outputFileName = Path.Combine(filePath, args[1]);
// read data, count chars
var count = Count(File.ReadAllText(inputFileName));
// create output content
var outPut = count.Select(x => $"{x.Key}\t{x.Value}");
// write it to the output file
File.WriteAllLines(outputFileName, outPut);
}
}
}
In the program class is where the sorted dictionary is. Is there a way to instead use an array instead of a sorted dictionary? What code would be best for doing this?
Thanks for the help.
Yes, and if its ASCII it will even run faster than using a dictionary without significantly increasing memory usage; simply use a
ulong[127]
array and index into it using the character's value:If you only need to handle printable ASCII characters (value 32 onwards), then you can make the array smaller and use offset 32 to index into it:
And offset again to print them out: