maxSpan function implementation The original question is as follows:

Consider the leftmost and righmost appearances of some value in an array. We'll say that the &quot;span&quot; is the number of elements between the two inclusive. A single value has a span of 1. Returns the largest span found in the given array. (Efficiency is not a priority.)
maxSpan([1, 2, 1, 1, 3]) → 4 
maxSpan([1, 4, 2, 1, 4, 1, 4]) → 6 
maxSpan([1, 4, 2, 1, 4, 4, 4]) → 6 

Here is my implementation of the maxSpan function.
public int maxSpan(int[] nums) {
  Map&lt;Integer, List&lt;Integer&gt;&gt; spans = new HashMap&lt;&gt;();
  for (int i = 0; i &lt; nums.length; i++) {
    if (!spans.containsKey(nums[i])) {
      List&lt;Integer&gt; position = new ArrayList&lt;&gt;();
      position.add(i);
      spans.put(nums[i], position);
    } else {
      List&lt;Integer&gt; position = spans.get(nums[i]);
      position.add(i);
    }
  }
  int globalMaxSpan = 0; // empty set has maxSpan = 0
  for (List&lt;Integer&gt; value : spans.values()) { //iterating over values only
      int localMaxSpan = 0;
      if (value.size() &gt; 1) {
        localMaxSpan = value.get(value.size() - 1) - value.get(0) + 1;
      } else localMaxSpan = 1;
      if (localMaxSpan &gt; globalMaxSpan)
        globalMaxSpan = localMaxSpan;
  }
  return globalMaxSpan;
}

Here is another implementation of the same algorithm I found on GitHub implemented by others.
public int maxSpan(int[] nums) {
    int max = 0;
    for(int i = 0; i &lt; nums.length; i++) {
        int j = nums.length - 1;
        while(nums[i] != nums[j])
            j--; 
        int span = j - i + 1;                                 
        if(span &gt; max)
            max = span;
    }                                             
    return max;
}

Although my implementation is O(n) runtime but it is more complicated than the below implementation. Please give comments to my code in different aspects. Thanks.
Your choice of data structures is unnecessarily heavy. You're interested only in the first and last index of a number, so store only those in a lightweight data structure of two integers (an array):

for (int i = 0; i &lt; nums.length; i++) {
    final int num = nums[i];
    int[] span = spans.get(num);
    if (span == null) {
        span = new int[] { i, i };
        spans.put(num, span);
    } else {
        span[1] = i;
    }
}


Java comes with built in functions for selecting larger or smaller number from two, so use them instead of if-statements or ternary operations. It makes code easier to read.

// Empty set has maxSpan = 0
int maxSpan = 0;
for (int[] span: spans.values()) {
    maxSpan = Math.max(maxSpan, span[1] - span[0] + 1);
}
return maxSpan;


Avoid end-of-line comments. They make the code messy and are a pain in the ass to maintain. Avoid unnecessary comments that describe what code does. The reader can already see that the code iterates over values. Instead document why the code does what it does.

Use variable names that describe what the variable is used for. Value does not give any new information to the reader about what it contains.

Position as a name for a list is deceptive. It's singular form suggests that it contains one value when it instead contains all positions of a certain number.

Edit: You don't actually even need to store the last occurrence of a number. Just store the first one and calculate the maxSpan as you traverse the array the first time.

int maxSpan = 0;
for (int i = 0; i &lt; nums.length; i++) {
    final int num = nums[i];
    Integer firstIndex = firstIndexes.get(num);
    if (firstIndex == null) {
        firstIndex = i;
        firstIndexes.put(num, firstIndex);
    }
    maxSpan = Math.max(maxSpan, i - firstIndex + 1);
}
return maxSpan;