title: What's the fastest way to extract non-zero indices from a byte array in C++
body: <p>I have a byte array </p>
  
  <pre><code>unsigned char* array=new unsigned char[4000000];
   ...
  </code></pre>
  
  <p>And I would like to get indices of all non-zero elements of the array. </p>
  
  <p>Of course, I can do following</p>
  
  <pre><code>for(int i=0;i&lt;size;i++)
  {
      if(array[i]!=0) somevector.push_back(i);
  }
  </code></pre>
  
  <p>Is there any faster algorithm than this?</p>
  
  <p><strong>Update 1</strong> I can see majority answer is no. I hoped that there is some magical bit operations I am not aware of. Some guys suggested sorting but no it's not feasible in this case. But thanks a lot for all your answers.</p>
  
  <p><strong>Update 2</strong> After 4 years and 4 months since this question posted, @wim suggested <a href="https://stackoverflow.com/a/41958528/749973">this answer that looks promising</a>.</p>