我有两个列表,分别是“ src”和“ dest”。 src包含一些重复项。我需要确定重复元素的索引。识别索引后,我想从“目标”列表的相同索引位置获取元素
最佳答案
这是执行此操作的算法:
1. Create a HashMap <Integer,List<Integer> > hm = new HashMap <Integer,List<Integer> >();
2. Iterate through the source list and fill this HashMap such that The key is the each number that you see and value will be a list that contains the list of indexes
int counter = 0;
for(Integer number : src){
If(hm.contains(number){
List<Integer> l = hm.get(number);
l.append(counter);
}
else{
List<Integer> l = new List<Integer>();
l.add(counter);
}
}
3. Using these that were stored in Hashmap to (print / fetch) the elements of destination list.