我有一个Integer数组,我想从中返回最小数字外观的索引值。
Integer[] array = {8,2,10,7,2,10};
所以,在这种情况下,如果我查找数字2,我发现{2}包含在array[1]
和array[4]
中。我在管理阵列方面有点迷失。
到目前为止,我所做的是检查给定的数字是否存在
public int exist( Integer number ) {
int index = 0;
int position = 0;
while (position <= array.length && number == array[position]){
index = index + 1;
if( index <= array.length ){
index = position ;
} else {
index = -1;
}
}
return index ;
}
我在Internet上找到了这个代码,它找到了整数数组中的第一个重复元素
class Main {
// This function prints the first repeating element in arr[]
static void printFirstRepeating(int arr[]) {
// Initialize index of first repeating element
int min = -1;
// Creates an empty hashset
HashSet<Integer> set = new HashSet<>();
// Traverse the input array from right to left
for (int i=arr.length-1; i>=0; i--) {
// If element is already in hash set, update min
if (set.contains(arr[i]))
min = i;
else // Else add element to hash set
set.add(arr[i]);
}
// Print the result
if (min != -1)
System.out.println("The first repeating element is " + arr[min]);
else
System.out.println("There are no repeating elements");
}
// Driver method to test above method
public static void main (String[] args) throws java.lang.Exception {
int arr[] = {10, 5, 3, 4, 3, 5, 6};
printFirstRepeating(arr); // output 5
}
}
我仍然无法将此代码与第一个代码结合起来以获得我真正想要的内容。
答案 0 :(得分:2)
解决此问题的一种方法是使用Map
代替HashSet
。
创建一个Map<Integer,Integer>
,将数组中的值映射到该数字的第一个匹配项。浏览数组,如果值已存在,请检查地图。
搜索代码如下所示:
Map<Integer,Integer> firstAppearance = new HashMap<>();
for (int i = 0 ; i != arr.length ; i++) {
if (firstAppearance.containsKey(arr[i])) {
return firstAppearance.get(arr[i]);
} else {
firstAppearance.put(arr[i], i);
}
}
答案 1 :(得分:1)
在您的编码示例10, 5, 3, 4, 3, 5, 6
中,第一个重复的值在索引4处为3
,在索引2处重复3
。其他重复索引为5时值为5
,索引为1的5
重复。
画报:
10, 5, 3, 4, 3, 5, 6
↑ at index 4: 3 is first repeated value
↑ at index 2: 3 was first found here
↑ at index 5: 5 is second repeated value
↑ at index 1: 5 was first found here
所以,可能的结果,取决于你真正想要的东西:
以下是这三种解决方案:
private static void printIndexOfFirstRepeated(int ... values) {
Set<Integer> set = new HashSet<>();
for (int i = 0; i < values.length; i++)
if (! set.add(values[i])) { // add() returns false if value already in set
System.out.println("Index of first repeated: " + i);
return;
}
System.out.println("No repeat found");
}
private static void printIndexOfRepeatedFirst(int ... values) {
Map<Integer, Integer> mapValueToIndex = new HashMap<>();
for (int i = 0; i < values.length; i++) {
Integer prevIndex = mapValueToIndex.put(values[i], i); // put() returns old value, or null
if (prevIndex != null) {
System.out.println("Index of repeated first: " + prevIndex);
return;
}
}
System.out.println("No repeat found");
}
private static void printFirstIndexOfRepeated(int ... values) {
Integer firstIndex = null;
Map<Integer, Integer> mapValueToIndex = new HashMap<>();
for (int i = 0; i < values.length; i++) {
Integer prevIndex = mapValueToIndex.put(values[i], i); // put() returns old value, or null
if (prevIndex != null && (firstIndex == null || prevIndex < firstIndex))
firstIndex = prevIndex;
}
if (firstIndex != null)
System.out.println("First index of repeated: " + firstIndex);
else
System.out.println("No repeat found");
}
<强> TEST 强>
printIndexOfFirstRepeated(10, 5, 3, 4, 3, 5, 6);
printIndexOfRepeatedFirst(10, 5, 3, 4, 3, 5, 6);
printFirstIndexOfRepeated(10, 5, 3, 4, 3, 5, 6);
<强>输出强>
Index of first repeated: 4
Index of repeated first: 2
First index of repeated: 1