Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Duplicate Character Counter in Beef #3709

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions archive/b/beef/DuplicateCharacterCounter.bf
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;

namespace DuplicateCharacterCounter;

class Program
{
public static void Usage()
{
Console.WriteLine("Usage: please provide a string");
Environment.Exit(0);
}

public static void CountDuplicateCharacters(StringView str, uint[] dupeCounter)
{
for (int i < 256)
{
dupeCounter[i] = 0;
}

for (char8 ch in str)
{
dupeCounter[(int)ch]++;
}
}

public static void ShowDuplicateCharacterCounts(StringView str, uint[] dupeCounter)
{
bool hasDupes = false;
for (char8 ch in str)
{
uint dupeCount = dupeCounter[(int)ch];
if (dupeCount > 1)
{
Console.WriteLine($"{ch}: {dupeCount}");
dupeCounter[(int)ch] = 0; // Indicate character already seen
hasDupes = true;
}
}

if (!hasDupes)
{
Console.WriteLine("No duplicate characters");
}
}

public static int Main(String[] args)
{
if (args.Count < 1 || args[0].Length < 1)
{
Usage();
}

uint[] dupeCounter = scope uint[256];
CountDuplicateCharacters(args[0], dupeCounter);
ShowDuplicateCharacterCounts(args[0], dupeCounter);
return 0;
}
}
Loading