'Sum all odd indexes in an array

I have to complete a program according to these instructions:

public class SumOddIndex {
    public static void main(String[] args){
       int[] array = {1,1,8,4,2,6};
       // Call the method sum, receive an integer value, and print the value on output. 
       // A call passing the example array should result in a value of 11. 
       // Use a while loop.
   }

Here code a method called sum. The method sum should take an int - array as argument and return the sum of all values in all array cells that have an odd index number. USE A WHILE- LOOP! }

This is what I have tried so far, but it is not working.

public static void main(String[] args) {
        int[] array = {1, 1, 8, 4, 2, 6};
        sum(array);
}

public static int sum(int[] y){
   int sum = 0;
   int i = 0;
   while(y.len

   public static int sum(int[] y) {
      int sum = 0;
      int i = 0;
      while (y.length%2 == 1) {
         //y.length%2 == 1;
         sum += y[i];
         i++;
      }
      System.out.println(y[i]);
      return i;
   }
}


Solution 1:[1]

Assuming you are using java, try something like this:

int i = 0;
int sum = 0;
while(i < myArray.length){
   sum += myArray[i];
   i++;
}

This is a while loop that will execute only as long as your counter variable is less than the size of the array (that way you will never have an index out of bounds). Inside the loop, it will add to the sum the value at the index of the counter.

Don't forget to increment your counter inside the while loop, or you will be stuck inside an infinite loop.

EDIT

I misread your question, but if you only want to sum odd values, try this:

while(i < myArray.length){
   if(myArray[i] % 2 == 1){ // If value is odd
      sum += myArray[i];
   }
   i++;
}

EDIT 2

To only sum odd indexes, change the above code to check if your counter is odd instead of the value at that index, like this:

while(i < myArray.length){
   if(i % 2 == 1){ // If index is odd
      sum += myArray[i];
   }
   i++;
}

Or, you could start you indexing at 1 and increase by 2 each time. I will only give psuedocode for this one:

// Start counter at 1
// While counter is less than array length
// Add element's value to the sum
// Increment counter by 2

Solution 2:[2]

Using Streams in Java 8 you can use IntStream and filter and reduce down to a total sum:

int[] array = {1,1,8,4,2,6};
int sum = IntStream.range(0, array.length).filter(i -> i % 2 == 1).map(i -> array[i]).sum();

Solution 3:[3]

I know this question has been answered already, but I thought it would still help out.

int i = 1;
int sum = 0;

while(i < myArray.length)
{            
    sum += myArray[i];  
    i += 2;
}

return sum;

I added @Logan Murphy's suggestion for optimization.

Solution 4:[4]

Use stream's filter and reduce operation.

int[] array = {1,1,8,4,2,6}; int sum = array.stream().filter(s -> s % 2 != 0).reduce(0, Integer::sum);

Solution 5:[5]

A bit optimized approach can be to calculate and store the length of the array in a variable once, instead of calculating it in each iteration.

public int oddIndSum(int [] myArray){
    int i = 1;
    int sum = 0;
    int length = myArray.length;
    while(i < length){
        if(i % 2 == 1){ // If index is odd then add
        sum += myArray[i];
        }
        i++;
    }
    return sum;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 Ckrempp
Solution 4 Lokesh Dau
Solution 5 Mohammad Wael