Given a set of N values that are all 0 or X, except for one Y, what is the most efficient way to find X?

StackOverflow https://stackoverflow.com/questions/11439337

  •  20-06-2021
  •  | 
  •  

Basically I have a set of redundant data that could have an error in one (bonus points: one or more) of the values. Some values could also be 0, which means ignore/invalid. What would be the most efficient way to return the "good" value?

The dumb solution would be a for loop that iterates over the set and returns once it finds the same non-zero value twice. But I feel like there might be some logical/bit-hacking expression that would be better.

有帮助吗?

解决方案

The 'dumb' solution might be the best one to use, especially if there aren't many zeros in the data set. You would break from the loop very early in most cases.

In the case where there are a lot of zeros, your speed can be optimized if your hardware is able to quickly scan for non-zero entries. I imagine that searching for non-zeros is very easy on FPGA hardware, but I don't have personal experience with this myself.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top