title: How to overcome an IndexOutOfBounds exception when iterating through pairs
body: <p>Suppose I have an array of <code>a, b, c</code>.</p>
  <pre><code>val arr = arrayOf(&quot;a&quot;, &quot;b&quot;, &quot;c&quot;)
  </code></pre>
  <p>My task is to eliminate <code>a</code> and <code>b</code> pairs.
  My approach here is as follows. I iterate through array and check what the next element is. If it's <code>b</code> then I remove it.</p>
  <pre><code>for (a in arr.indices) {
              when (arr[a]) {
                  &quot;a&quot; -&gt; {
                      if (arr[a + 1] == &quot;b&quot;){/replacement part}
  </code></pre>
  <p>This gives me an IndexOutOfBoundsException when I pass</p>
  <pre><code>val arr = arrayOf(&quot;a&quot;) 
  </code></pre>
  <p>It is obviously caused by the if condition where I check <code>arr[a + 1]</code>, which doesn't exist in my array. What should be my approach to overcome this difficulty?</p>