卸下整数数组复制 [英] Remove duplicates from integer array
本文介绍了卸下整数数组复制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我具有编码这是一个问题:
写 removeDuplicates
名为静态方法作为输入整数并返回数组作为结果的整数去除所有重复的新数组。
例如,如果输入数组元素{4,3,3,4,5,2,4}所产生的阵列
应{4,3,5,2}
这是我迄今所做
公共静态INT [] removeDuplicates(INT [] S){
INT [] K =新INT [s.length]
K表[0] = S [0];
INT M = 1;
的for(int i = 1; I< s.length; ++ I){
如果(S [I]!= S [I-1]){
K表[M] = S [I]
++米;
}//万一
} // endFori
复位K;
} // endMethod
解决方案
试试这个 -
公共静态INT [] removeDuplicates(INT [] S){
INT结果[] =新的INT [s.length],J = 0;
对于(INT I:秒){
如果(!isExists(结果,I))
结果[J ++] =我;
}
返回结果;
}
私有静态布尔isExists(INT []数组,int值){
对于(INT I:数组){
如果(我==值)
返回true;
}
返回false;
}
I having a problem with coding this:
Write a static method named removeDuplicates
that takes as input an array of integers and returns as a result a new array of integers with all duplicates removed.
For example, if the input array has the elements {4, 3, 3, 4, 5, 2, 4} the resulting array
should be {4, 3, 5, 2}
Here's what I have done so far
public static int[] removeDuplicates(int []s){
int [] k = new int[s.length];
k[0]=s[0];
int m =1;
for(int i=1;i<s.length;++i){
if(s[i]!=s[i-1]){
k[m]=s[i];
++m;
}//endIF
}//endFori
return k;
}//endMethod
解决方案
try this -
public static int[] removeDuplicates(int []s){
int result[] = new int[s.length], j=0;
for (int i : s) {
if(!isExists(result, i))
result[j++] = i;
}
return result;
}
private static boolean isExists(int[] array, int value){
for (int i : array) {
if(i==value)
return true;
}
return false;
}
这篇关于卸下整数数组复制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文