Grokking Multithreading and Concurrency for Coding Interviews

0% completed

Problem 3: Linear Search with Indices and Occurrences

Overview

To enhance the linear search to find and count all occurrences, we'll store each occurrence in a shared collection and count the total number of occurrences in the end. This method will provide both the indices where the target element appears and the total number of appearances in the array. With multithreading, each thread will examine its chunk of the array, reducing the overall time for the search.

.....

.....

.....

Like the course? Get enrolled and start learning!
S

Shlomi Fisher

· 2 years ago

Hi,

The occurrencesCount global variable seems redundant. Why not just use foundIndices.size()?

Even if we still want to keep using occurrencesCount, then localCount is redundant, since we can use localIndices.size() instead. This would make the countLock mutex redundant.

Best regard

Show 1 reply
Aldemar Hernández

Aldemar Hernández

· 2 years ago

It wouldn't be easier just call count property of the list ?

Console.WriteLine($"Element found { foundIndexes.Count } times at indices: " + string.Join(' ', foundIndexes));