检查 int 数组中是否存在另一个 int 实例
Check if another instance of int exists in an int array
假设我有一个名为 sequence 的 int 数组
[1, 3, 2, 4, 3, 5, 4, 6, 1, 3]
我想检查 1
的另一个实例是否存在于数组中的第一个实例之外,如果存在,我想获取它在数组中的索引。
到目前为止,这是我所做的:
// Get the first and second number of the sequence array
int firstNumberIndex = 0;
int firstNumber = sequence[0];
int secondNumber = sequence[1];
// Check that firstNumber < secondNumber
if (firstNumber < secondNumber) {
// Remove firstNumber from the sequence array
instance = removeElement(sequence, firstNumberIndex);
// Check whether another instance of
// firstNumber exists in sequence array
if (contains(sequence, firstNumber)) {
// Here, I would like to get the index of
// the other instance of '1', for example
}
}
可读性优于原始执行速度:
List<Integer> list = new ArrayList<>();
IntStream.of(sequence).forEach(list::add);
int index = list.subList(1, list.size()).indexOf(sequence[0]) + 1;
如果没有第二次出现,index
将是 0
。
快速回答使用哈希:
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class Snippet {
public static void main(String[] args) {
int[] sequence = {1, 3, 2, 4, 3, 5, 4, 6, 1, 3};
Map<Integer, List<Integer>> indexMap = new LinkedHashMap<>();
for (int i = 0; i < sequence.length; i++) {
int val = sequence[i];
indexMap.computeIfAbsent(val, k -> new ArrayList<>()).add(i);
}
System.out.println(indexMap);
}
}
输出:
{1=[0, 8], 3=[1, 4, 9], 2=[2], 4=[3, 6], 5=[5], 6=[7]}
所以它给出序列中的每个值所有索引,在该处找到这个值,可能是也可能不是超出了 OP 的要求。
按遇到的顺序返回值(感谢 LinkedHashMap),并对索引进行排序。总的来说,它的时间是O(序列长度)。
- 您可以使用
List#indexOf
方法两次来获取对象在 List<Integer>
中的第二次出现:
public static int getSecond(List<Integer> list, int val) {
// index of the first occurrence
int i = list.indexOf(val);
// if found and not the last one
if (i > -1 && i < list.size() - 1) {
// index of the second occurrence from
// the sublist after the first occurrence
int j = list.subList(i + 1, list.size()).indexOf(val);
// if found, return the composite index
if (j > -1) return j + i + 1;
}
// not found
return -1;
}
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1, 3, 2, 4, 3, 5, 4, 6, 1, 3);
System.out.println(getSecond(list, 3)); // 4
System.out.println(getSecond(list, 1)); // 8
System.out.println(getSecond(list, 6)); // -1
System.out.println(getSecond(list, 7)); // -1
}
- 如果只需要一个数组
int[]
,您可以使用 IntStream
:
public static int getSecond(int[] arr, int val) {
return IntStream
// iterating over array indices
.range(0, arr.length)
// filter the search elements
.filter(i -> arr[i] == val)
// skip the first one
.skip(1)
// take the second one
.findFirst()
// if not found
.orElse(-1);
}
public static void main(String[] args) {
int[] arr = {1, 3, 2, 4, 3, 5, 4, 6, 1, 3};
System.out.println(getSecond(arr, 3)); // 4
System.out.println(getSecond(arr, 1)); // 8
System.out.println(getSecond(arr, 6)); // -1
System.out.println(getSecond(arr, 7)); // -1
}
假设我有一个名为 sequence 的 int 数组
[1, 3, 2, 4, 3, 5, 4, 6, 1, 3]
我想检查 1
的另一个实例是否存在于数组中的第一个实例之外,如果存在,我想获取它在数组中的索引。
到目前为止,这是我所做的:
// Get the first and second number of the sequence array
int firstNumberIndex = 0;
int firstNumber = sequence[0];
int secondNumber = sequence[1];
// Check that firstNumber < secondNumber
if (firstNumber < secondNumber) {
// Remove firstNumber from the sequence array
instance = removeElement(sequence, firstNumberIndex);
// Check whether another instance of
// firstNumber exists in sequence array
if (contains(sequence, firstNumber)) {
// Here, I would like to get the index of
// the other instance of '1', for example
}
}
可读性优于原始执行速度:
List<Integer> list = new ArrayList<>();
IntStream.of(sequence).forEach(list::add);
int index = list.subList(1, list.size()).indexOf(sequence[0]) + 1;
如果没有第二次出现,index
将是 0
。
快速回答使用哈希:
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class Snippet {
public static void main(String[] args) {
int[] sequence = {1, 3, 2, 4, 3, 5, 4, 6, 1, 3};
Map<Integer, List<Integer>> indexMap = new LinkedHashMap<>();
for (int i = 0; i < sequence.length; i++) {
int val = sequence[i];
indexMap.computeIfAbsent(val, k -> new ArrayList<>()).add(i);
}
System.out.println(indexMap);
}
}
输出:
{1=[0, 8], 3=[1, 4, 9], 2=[2], 4=[3, 6], 5=[5], 6=[7]}
所以它给出序列中的每个值所有索引,在该处找到这个值,可能是也可能不是超出了 OP 的要求。
按遇到的顺序返回值(感谢 LinkedHashMap),并对索引进行排序。总的来说,它的时间是O(序列长度)。
- 您可以使用
List#indexOf
方法两次来获取对象在List<Integer>
中的第二次出现:public static int getSecond(List<Integer> list, int val) { // index of the first occurrence int i = list.indexOf(val); // if found and not the last one if (i > -1 && i < list.size() - 1) { // index of the second occurrence from // the sublist after the first occurrence int j = list.subList(i + 1, list.size()).indexOf(val); // if found, return the composite index if (j > -1) return j + i + 1; } // not found return -1; } public static void main(String[] args) { List<Integer> list = Arrays.asList(1, 3, 2, 4, 3, 5, 4, 6, 1, 3); System.out.println(getSecond(list, 3)); // 4 System.out.println(getSecond(list, 1)); // 8 System.out.println(getSecond(list, 6)); // -1 System.out.println(getSecond(list, 7)); // -1 }
- 如果只需要一个数组
int[]
,您可以使用IntStream
:public static int getSecond(int[] arr, int val) { return IntStream // iterating over array indices .range(0, arr.length) // filter the search elements .filter(i -> arr[i] == val) // skip the first one .skip(1) // take the second one .findFirst() // if not found .orElse(-1); } public static void main(String[] args) { int[] arr = {1, 3, 2, 4, 3, 5, 4, 6, 1, 3}; System.out.println(getSecond(arr, 3)); // 4 System.out.println(getSecond(arr, 1)); // 8 System.out.println(getSecond(arr, 6)); // -1 System.out.println(getSecond(arr, 7)); // -1 }