C# – Change a dictionary’s values in a foreach loop

In .NET 5 and above, you can loop through a dictionary and directly change its values. Here’s an example: This outputs the following: You couldn’t do this before .NET 5, because it would invalidate the enumerator and throw an exception: InvalidOperationException: Collection was modified; enumeration operation my not execute. Instead, you’d have to make the … Read more

HackerRank – Zig Zag Sequence solution

In this article, I’ll explain the Zig Zag Sequence algorithm problem on HackerRank. Problem statement: You’re given an integer array with an odd number of elements (ex: [5, 2, 3, 1, 4]). You need to re-arrange the elements so they’re in a zig zag sequence, which means: Here’s a diagram to help you visualize what … Read more

HackerRank – Mark and Toys solution

In this article, I’ll explain how to solve the Mark and Toys algorithm problem on HackerRank. Problem statement: Given a fixed budget and a list of item prices. What is the max number of items you can purchase? You can only buy each item once. Note: This is the Mark and Toys problem from HackerRank. … Read more