-
呼如林
public static void main(String[] args) {
int value = 0;
int[] aa = { 1, 2, 3, 4, 5, 2, 3, 4, 2 };
Map<Integer, Integer> m = new LinkedHashMap<Integer, Integer>();
// 将每个数字对应的个数放入map
for (int i = 0; i < aa.length; i++) {
if (m.containsKey(aa[i])) {
m.put(aa[i], ((Integer) m.get(aa[i])) + 1);
} else {
m.put(aa[i], 1);
}
}
// 遍历map
Iterator<Map.Entry<Integer, Integer>> it = m.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Integer, Integer> entry = it.next();
if (entry.getValue() == 2) {
value = entry.getKey();
break;
}
}
System.out.println(value);
}
-
回首忆惘然
public Integer getFirstDoubleRepetitiveltem(int[] items){
for (int i = 1; i < items.length; i++)
for (int j = 0; j < i; j++)
if (items[i] == item[j]) return new Integer(items[i]);
return null;
}当然,两重循环的效率会比较低,如果需要效率比较高的算法,采纳了,可以帮你写。
-
慕尼黑的夜晚无繁华
public static Integer getFirstDoubleRepetitiveltem(int[] items){
for (int i = 0,len = items.length;i < len;i++) {
if(isRepeat(items,i)){
return items[i];
}
}
return null;
}
public static boolean isRepeat(int[] items,int focus){
for (int i = 0;i < focus;i++) {
if (items[i] == items[focus]){
return true;
}
}
return false;
}
-
开满天机
private static int method(int[] is){int a=0;for(int i=0;i<is.length-1;i++){for(int j=i+1;j<is.length;j++){if(is[i]==is[j]){return is[i];}}}return a;}
-
哈士奇WWW
public static Integer getFirstDoubleRepetitiveltem(int[] items){for (int i = 0,len = items.length;i < len;i++) {if(isRepeat(items,i)){return items[i];}}return null;}public static boolean isRepeat(int[] items,int focus){
for (int i = 0;i < focus;i++) {
if (items[i] == items[focus]){
return true;
}
}
return false;
}
-
饮歌长啸
public int getFirstDoubleRepetitiveltem(int[] intArr){
int temp = 0;
aa:
for(int i= 0; i<intArr.length; i++) {
for(int j= i+1; j<intArr.length; j++) {
if(intArr [i] == intArr[j]) {
temp = intArr[i];
break aa;
}
}
}
return 0;
}