## The challenge

The objective of Duck, duck, goose is to walk in a circle, tapping on each player’s head until one is chosen.

Task: Given an array of Player objects (an array of associative arrays in PHP) and an index (1-based), return the `name` of the chosen Player(`name` is a property of `Player` objects, e.g `Player.name`)

Example:

 ``````1 2 3 `````` ``````duck_duck_goose([a, b, c, d], 1) should return a.name duck_duck_goose([a, b, c, d], 5) should return a.name duck_duck_goose([a, b, c, d], 4) should return d.name ``````

## The solution in Java code

Option 1 (`returning the modulo`):

 ``````1 2 3 4 5 `````` ``````public class Solution { public static String duckDuckGoose(Player[] players, int goose) { return players[(goose - 1) % players.length].name; } } ``````

Option 2 (`slightly longer`):

 ``````1 2 3 4 5 6 7 8 `````` ``````public class Solution { public static String duckDuckGoose(Player[] players, int goose) { int a = players.length; int b = goose%a-1; if(b<0) b = b+a; return players[b].name; } } ``````

Option 3 (`working through the problem`):

 `````` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 `````` ``````public class Solution { public static String duckDuckGoose(Player[] players, int goose) { int len = players.length; int diff = 0; if(goose > 0) { if(goose % len == 0){ return players[len-1].name; } if(goose > len){ diff = (goose % len) - 1; } else if(goose == 1) { diff = 0; } if(goose < len){ diff = goose - 1; } if(goose == len){ return players[len-1].name; } } return players[diff].name; } } ``````

Option 4 (`using a while`):

 ``````1 2 3 4 5 6 7 8 9 `````` ``````public class Solution { public static String duckDuckGoose(Player[] players, int goose) { int size = players.length ; while (goose > size) { goose = goose - size; } return players[goose-1].name; } } ``````

## Test cases to validate our Java solution

 `````` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 `````` ``````import org.junit.Test; import static org.junit.Assert.assertEquals; import org.junit.runners.JUnit4; public class ExampleSolutionTests { @Test public void tests() { Player[] players = makePlayerArr(new String[] {"a", "b", "c", "d", "c", "e", "f", "g", "h", "z"}); assertEquals("a", Solution.duckDuckGoose(players, 1)); assertEquals("c", Solution.duckDuckGoose(players, 3)); assertEquals("z", Solution.duckDuckGoose(players, 10)); assertEquals("z", Solution.duckDuckGoose(players, 20)); assertEquals("z", Solution.duckDuckGoose(players, 30)); assertEquals("g", Solution.duckDuckGoose(players, 18)); assertEquals("g", Solution.duckDuckGoose(players, 28)); assertEquals("b", Solution.duckDuckGoose(players, 12)); assertEquals("b", Solution.duckDuckGoose(players, 2)); assertEquals("f", Solution.duckDuckGoose(players, 7)); } private Player[] makePlayerArr(String[] names) { Player[] players = new Player[names.length]; for (int i = 0; i < names.length; i++) { players[i] = new Player(names[i]); } return players; } } ``````