Concise O(N) JAVA solution 9 lines of code

  • 0

    The idea is to use a hashtable since they told us that a duplicate does not exist in the array.

    class Solution {
    public int search(int[] nums, int target) {
    HashMap<Integer,Integer> indexes = new HashMap<Integer,Integer>();
    for (int i = 0; i < nums.length; i++){
    if (indexes.containsKey(target)) return indexes.get(target);
    else return -1;


Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.