public class Solution { // example in leetcode book public int[] twoSum(int[] nums, int target) { Map map = new HashMap<>(); for (int i = 0; i < nums.length; i++) { int x = nums[i]; if (map.containsKey(target - x)) { return new int[]{map.get(target - x), i}; } map.put(x, i); } throw new IllegalArgumentException("No two sum solution"); } }