[英]How to copy non-duplicated values from one array to the other?
我正在使用第一個數組來存儲所有數字數據庫,其中一些數字是重復的。
我已經遍歷了這個數組,以查看哪些項目被重復,並將重復項目的索引添加到第二個數組。
現在,我必須遍歷第一個數組並將所有重復的值添加到第三個數組(假設我們知道哪些字段是重復的)。
但是如何正確地做到這一點? 我不能停止從第一數組到第三數組的每一項添加。
假設我不能使用HashSet()。
這樣做的目的是演示如何將一個數組移動到另一個數組,並刪除重復時間為O(N)的重復項。
Input numbers: 00, 11, 11, 22, 33, 44, 55, 55, 66, 77, 88, 99
Output which index are duplicated: 1, 2, 6, 7
Output I get: 00, 11, 11, 22, 33, 44, 55, 55, 66, 77, 88, 99 (same as the input)
碼:
public void dups()
{
int[] b = new int[100];
int[] c = new int[100];
int k = 0;
int n = 0;
int p = 0;
for (int i = 0; i < nElems; i++)
for (int j = 0; j < nElems; j++)
if(a[j].equals(a[i]) && j != i)
b[k++] = i;
for (int l = 0; l < k; l++)
System.out.print(b[l] + " ");
for (int m = 0; m < nElems; m++)
if (m != b[p + 2])
c[m] = (Integer) a[n++];
System.out.print("\n");
for (int o = 0; o < nElems; o++)
System.out.print(c[o] + " ");
}
您可以使用HashSet而不是Array來存儲所有數字數據庫。 之后,使用Collections.toArray()獲得所需的數組。
我看到問題已被編輯,我們不再想要使用HashSet。 無論如何,這里已經解決了您的問題, 算法:從數組中刪除重復整數的有效方法
可以用一種更簡單的方法來完成:
Set<Integer> uniqueSet = new HashSet<Integer>();
uniqueSet.addAll(list);
//not uniqueSet contains only unique elements from the list.
它起作用的原因是Set不能包含重復項。 因此,在向Set中添加元素時,它會忽略重復的元素。
除了標記所有重復項,您還可以標記所有先前已經看到的。
代替:
for (int i = 0; i < nElems; i++)
for (int j = 0; j < nElems; j++)
if(a[j].equals(a[i]) && j != i)
b[k++] = i;
使用類似:
for (int i = 0; i < nElems; i++)
for (int j = i+1; j < nElems; j++)
if(a[j].equals(a[i]))
b[k++] = j;
然后,您應該看到:
Output which index are duplicated: 2, 7
哪個應該更容易使用。
這是一個可行的解決方案-盡管我不會這樣做:
public class Test {
Integer[] a = {00, 11, 11, 22, 33, 44, 55, 55, 66, 77, 88, 99};
int nElems = a.length;
public void dups() {
int[] b = new int[100];
int[] c = new int[100];
int k = 0;
int n = 0;
int p = 0;
for (int i = 0; i < nElems; i++) {
for (int j = i + 1; j < nElems; j++) {
if (a[j].equals(a[i])) {
b[k++] = j;
}
}
}
for (int l = 0; l < k; l++) {
System.out.print(b[l] + " ");
}
for (int m = 0; m < nElems; m++) {
if (m != b[p]) {
c[n++] = a[m];
} else {
p += 1;
}
}
System.out.print("\n");
for (int o = 0; o < nElems - k; o++) {
System.out.print(c[o] + " ");
}
}
public static void main(String args[]) {
new Test().dups();
}
}
打印:
2 7
0 11 22 33 44 55 66 77 88 99
我不知道這是您要找的東西。 但是它刪除重復項。 試一試,
int[] b = { 00, 11, 11, 22, 33, 44, 55, 55, 66, 77, 88, 99 };
List<Integer> noDups = new ArrayList<Integer>();
Boolean dupliceExists;
for (int i = 0; i < b.length; i++) {
dupliceExists = Boolean.FALSE;
for (Integer integ : noDups) {
if (Integer.valueOf(b[i]).equals(integ)) {
dupliceExists = Boolean.TRUE;
//Get index if you need the index of duplicate from here
}
}
if (!dupliceExists) {
noDups.add(b[i]);
}
}
for (int i = 0; i < noDups.size(); i++) {
System.out.println(noDups.get(i));
}
我將進行編碼,以將非重復的元素從一個數組復制到另一個數組。
/*
* print number of occurance of a number in an array beside it
*
* */
class SpoorNumberOfOccuranceInArray{
public static void main(String[] args){
int[] arr={65,30,30,65,65,70,70,70,80};
int[] tempArr=new int[arr.length];//making size compatible to source Array.
int count=0;
for(int i=0;i<arr.length;i++){
int temp=arr[i];
for(int j=0;j<tempArr.length;j++){
if(temp==tempArr[j]){ //Comparing the Availability of duplicate elements in the second Array.---------
break;
}
else{ //Inserting if value is not in the second Array.---------------
if( tempArr[j]==0){
tempArr[j]=temp;
break;
}//end if
}//end else
}//end if
}//end for
for(int i=0;i<tempArr.length;i++){
for(int j=0;j<arr.length;j++){
if(tempArr[i]==arr[j])
count++;
}
System.out.println(tempArr[i]+" "+count);
count=0;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.