我最近参加了一次面试,面试内容包括以下问题。请提供可能的解决方案。
在Java中编写一个方法来查找整数数组中的重复元素,而不使用嵌套循环( for/ while / do while等),也不使用库函数或标准API。
发布于 2015-07-18 14:43:06
嘿,下面的解决方案的复杂度是O(n),而且效果很好。检查它是否有帮助。
public class Main {
public static void main(String[] args) {
int a[] = new int[]{10,3,5,10,5,4,6};
String distinctElement="";
String repetitiveTerms="";
for(int i=0;i<a.length;i++){
if(i==0){
distinctElement+=a[i]+" ";
}
else if(distinctElement.contains(""+a[i])){
repetitiveTerms+=a[i]+" ";
}
else{
distinctElement+=a[i]+" ";
}
}
}
}https://stackoverflow.com/questions/31488494
复制相似问题