title: Optimization array operations
body: <p>I have task to optimize amount of iterations over array.</p>
  
  <p>I have such source array:</p>
  
  <pre><code>int[] dataArray = new int[10] {0, 1, 1, 1, 0, 0, 0, 0, 1, 0};
  </code></pre>
  
  <p>Where 0-no feature, 1 - feature present.
  This </p>
  
  <pre><code>public struct MyDataStruct
  {
      public int Index; //index in array
      public int Value; //some value
  }
  </code></pre>
  
  <p>There are also two ways of reprezentation of some feature: ideally I need to use ONLY OptimizedKeyValueArray1 and to remove all usings of NotOptimizedKeyValueArray1.</p>
  
  <pre><code>public static readonly MyDataStruct[] NotOptimizedKeyValueArray1 =
          {
                  new MyDataStruct(0, -1),
                  new MyDataStruct(1, 1000),
                  new MyDataStruct(2, 2500),
                  new MyDataStruct(3, 4500),
                  new MyDataStruct(4, -1),
                  new MyDataStruct(5, -1),
                  new MyDataStruct(6, -1),
                  new MyDataStruct(7, -1),
                  new MyDataStruct(8, 6500),
                  new MyDataStruct(9, -1)
          };
  
  public static readonly MyDataStruct[] OptimizedKeyValueArray1 =
          {
                  new MyDataStruct(1, 1000),
                  new MyDataStruct(2, 2500),
                  new MyDataStruct(3, 4500),
                  new MyDataStruct(8, 6500)
          };
  </code></pre>
  
  <p>As a result I need to have resultArray, the same quantity as inputArray:</p>
  
  <pre><code>int[] inputArray = new int[10] {0, 1, 1, 1, 0, 0, 0, 0, 1, 0};
  
  MyDataStruct[] resultDataSet = new MyDataStruct[10];
  </code></pre>
  
  <p>This is my solution of this task, but it is not optimized and not scalable, because I am iterate over the elements which are not important for me and I know about it from the very begining. As result I have 10 elements:</p>
  
  <pre><code>for(int i = 0; i &lt; dataArray.Length; i++)
  {
      resultDataSet[i] = new MyDataStruct(i, dataArray[i]);
  }
  </code></pre>
  
  <p>But SOMEHOW I need to make it more efficient and NOT iterate over the 0-value elements. But according this code, I will create only 4 elements, while I need to have other 6 elements with some default values. And I don't know how to do it more efficiently.</p>
  
  <pre><code>for(int i = 0; i &lt; OptimizedKeyValueArray1.Length; i++)
  {
      int index = OptimizedKeyValueArray1.Index;
      int value = OptimizedKeyValueArray1.Value;
  
      resultDataSet[i] = new MyDataStruct(i, dataArray[i]);
  }
  </code></pre>
  
  <p>Or maybe you can propose some more efficient algorithms over the array to do this task. Thanks.</p>