Java 如何将非重复值从一个数组复制到另一个数组?
我使用第一个数组来存储所有数字数据库,其中一些数字是重复的 我已经检查了这个数组以查看哪些项是重复的,并且正在将重复项的索引添加到第二个数组中 现在,我必须循环第一个数组,并将除了重复的值之外的所有值添加到第三个数组,假设我们知道哪些字段是重复的 但如何正确地做到这一点呢?我无法让它停止从第一个数组到第三个数组添加所有项目 假设我不能使用HashSet 其目的是演示如何在时间复杂度上将一个阵列移动到另一个阵列Java 如何将非重复值从一个数组复制到另一个数组?,java,arrays,loops,if-statement,duplicates,Java,Arrays,Loops,If Statement,Duplicates,我使用第一个数组来存储所有数字数据库,其中一些数字是重复的 我已经检查了这个数组以查看哪些项是重复的,并且正在将重复项的索引添加到第二个数组中 现在,我必须循环第一个数组,并将除了重复的值之外的所有值添加到第三个数组,假设我们知道哪些字段是重复的 但如何正确地做到这一点呢?我无法让它停止从第一个数组到第三个数组添加所有项目 假设我不能使用HashSet 其目的是演示如何在时间复杂度上将一个阵列移动到另一个阵列 Input numbers: 00, 11, 11, 22, 33, 44, 55,
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)
代码:
可以用更简单的方法完成:
Set<Integer> uniqueSet = new HashSet<Integer>();
uniqueSet.addAll(list);
//not uniqueSet contains only unique elements from the list.
它工作的原因是不能包含重复项。因此,在向集合中添加元素时,它会忽略那些重复的元素。可以用更简单的方法完成:
Set<Integer> uniqueSet = new HashSet<Integer>();
uniqueSet.addAll(list);
//not uniqueSet contains only unique elements from the list.
它工作的原因是不能包含重复项。因此,在向集合中添加元素时,它会忽略那些重复的元素。您可以使用哈希集合而不是数组来存储所有数字数据库。 然后使用Collections.toArray获取所需的数组 我看到问题被编辑了,我们不想再使用HashSet了。
无论如何,这里已经回答了您的问题,您可以使用哈希集而不是数组来存储所有数字数据库。 然后使用Collections.toArray获取所需的数组 我看到问题被编辑了,我们不想再使用HashSet了。
无论如何,您的问题已经在这里得到了解答,您可以标记前面已经看到的所有重复项,而不是标记所有重复项 而不是:
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;
这应该更容易处理
这里有一个可行的解决方案——尽管我不会这样做:
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();
}
}
您可以标记前面已经看到的所有内容,而不是标记所有重复项 而不是:
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;
这应该更容易处理
这里有一个可行的解决方案——尽管我不会这样做:
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();
}
}
我不知道这是不是你想要的。但它会删除重复的。试试看
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));
}
我不知道这是不是你想要的。但它会删除重复的。试试看
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;
}
}
}
我将编写代码,将非重复元素从一个数组复制到另一个数组
/*
* 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;
}
}
}
你真的想在数据库中保存整数吗?你能给出一些示例输入/输出吗?你真的想在数据库中保存整数吗?你能给出一些示例输入/输出吗?当设置策略正确时,类必须实现hashcode和equals方法。另外,在可以使用Set的情况下使用Set是一种不好的做法。他说它是整数,至于泛型,我试图快速回答;。。我想这是打字稿。让uniqueSet=新集合;虽然Set策略是正确的,但类必须实现hashcode和equals方法。另外,在可以使用Set的情况下使用Set是一种不好的做法。他说它是整数,至于泛型,我试图快速回答;。。我想这是打字稿。让uniqueSet=新集合;您应该首先尝试使用默认的helper方法,而不是重新发明控制盘。就像在代码中一样,您可以简单地忽略与“dupliceExists”相关的逻辑,并放置以下条件来控制重复项:-如果!noDups.containsInteger.valueOfb[i]{noDups.addb[i]}您应该首先尝试使用默认的helper方法,而不是重新发明控制盘。就像在代码中一样,您可以简单地忽略与“dupliceExists”相关的逻辑,并放置以下条件来控制重复项:-如果!noDups.containsInteger.valueOfb[i]{noDups.addb[i]}这正是我试图实现的目标!你甚至用数组b[]中的双重重复索引解决了我的问题。非常感谢:谢谢!请确保您了解我是如何做到这一点的,以及如何使用哈希集会更好。特别是为什么o