如何求无序数组中重复元素的最小距离?Java代码问题排查
Hey there! Let's fix your code step by step—you've got a couple of key issues causing that unexpected Integer.MAX_VALUE output, plus some logic mismatches with what you're trying to achieve.
First: Your min method is doing the opposite of what you think
You wrote this method to get the minimum of two numbers, but it actually returns the maximum:
public static int min(int a, int b){ if (a >b){ return a; } else { return b; } }
Since you initialize min to Integer.MAX_VALUE, every time you call this method, it will keep the larger value (which is always Integer.MAX_VALUE). That's exactly why your output is 2147483647—this method is broken for your use case. Fix it to return the smaller value instead, or just use Java's built-in Math.min():
public static int min(int a, int b){ return Math.min(a, b); }
Second: Your counter logic is tracking the wrong thing
Right now, you're incrementing counter when elements don't match, which counts how many non-duplicate elements come after array[i]—this has nothing to do with the distance between duplicate elements.
What you actually need to do is:
- When you find a duplicate (
array[i] == array[j]), calculate the distance asj - i(this is the standard index-based distance between two elements) - Immediately compare this distance to your current
minand updateminif the new distance is smaller
Third: You're updating min at the wrong time
You're only comparing the irrelevant counter to min after finishing the inner loop for each i. Instead, you should update min as soon as you find a valid duplicate pair.
Fixed Full Code
Here's the corrected version of your code that works as intended:
public class MinDuplicateDistance { public static void main(String[] args) { int[] array = {5, 3, 4, 2, 3, 4, 5, 7}; int min = Integer.MAX_VALUE; if (hasDuplicates(array)) { for (int i = 0; i < array.length; i++) { for (int j = i + 1; j < array.length; j++) { if (array[i] == array[j]) { int distance = j - i; min = min(distance, min); // Optional: Early exit if we find the smallest possible distance (1) if (min == 1) { break; } } } } System.out.println(min); // Outputs 3, the actual minimum distance for your array } else { System.out.println("-1"); } } public static boolean hasDuplicates(int[] array) { Set<Integer> seen = new HashSet<>(); for (int element : array) { if (seen.contains(element)) { return true; // No need to check further once we find a duplicate } seen.add(element); } return false; } public static int min(int a, int b) { return Math.min(a, b); } }
A Quick Note on Your Expected Result
You mentioned expecting a return value of 4, but based on your array {5, 3, 4, 2, 3, 4, 5, 7}:
- The duplicate pairs are:
- 5 at index 0 and 6 → distance 6
- 3 at index 1 and 4 → distance 3
- 4 at index 2 and 5 → distance 3
The actual minimum distance here is 3, not 4. Either your expected result was a typo, or you had a different definition of "distance" in mind (like counting elements between duplicates instead of index difference).
内容的提问来源于stack exchange,提问作者Faith Wilkins El

