如何检查数组中是否有重复的数字?(复制)

时间:2022-02-28 07:31:04

This question already has an answer here:

这个问题已经有了答案:

I have to find out if there are duplicate numbers in an array, and if there is a duplicate found I need to -5 points. Here is my code so far:

我需要知道数组中是否有重复的数字,如果有重复的,我需要-5点。这是我目前的代码:

for (int k = 0; k < arrNums2.length; k++) 
{
    for (int i = 0; i < arrNums2.length; i++) 
    {
        if (arrNums2[k] == arrNumsCompare[i])
        {             
            points = points - 5;            
            for (int j = 0; j < arrNums2.length; j++)
            {
                if (arrNums2[k] == arrNums2[j])
                {
                    arrNums2[j] = 0;
                }
            }
        }
    }
}

6 个解决方案

#1


4  

I suggest this one line solution :

我建议这一行解决方案:

Integer[] numbers = {1,2,3,4,5,6,5,4,3,2,1};

return 5 * (new HashSet<Integer>(Arrays.asList(numbers)).size() - numbers.length);

I add all the elements to a Set, which by definition refuses duplicates. Then I compare the size of the set to the length of the array and multiply the result by 5.

我将所有元素添加到一个集合中,该集合拒绝重复。然后我将集合的大小与数组的长度进行比较,并将结果乘以5。

#2


0  

You can work on this pseudo code:

你可以使用这个伪代码:

int nDuplicates = 0;
arrNums2 = arrNums2.Sort(); // Sort the array in ascending or descending order. 

for(int i =1; i<arrNums2.size(); ++i)
{ 
 if(arrNums2[i-1] == arrNums2[i])
 {
  nDuplicates++;
 }
}
Point -= nDuplicates*5;

Edit: If the input 5,5,5,5 has to be considered as a single duplication then use the following snippet.

编辑:如果必须将输入5、5、5视为单个重复,则使用以下代码片段。

    int nDuplicates = 0;
    arrNums2 = arrNums2.Sort(); // Sort the array in ascending or descending order. 
    int DuplicateNumber = 0;
       for(int i =1; i<arrNums2.size(); ++i)
        { 
         if(arrNums2[i-1] == arrNums2[i])
         {
          if(DuplicateNumber != arrNums2[i])
          {
           nDuplicates++;
           DuplicateNumber = arrNums2[i];
          }
         }
        }
     Point -= nDuplicates*5;

#3


0  

To find duplicates in one iteration over the original array without prior sorting you can use a HashMap to store each of the entries and increment count every time you find it again.
This way you can stop processing as soon as you find the first duplicate or continue to find all...

要在原始数组的一次迭代中找到重复,而不需要事先排序,您可以使用HashMap来存储每个条目,并在每次再次找到条目时增加计数。这样,您可以在找到第一个副本后立即停止处理,或者继续查找所有副本……

Edit: http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html

编辑:http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html

Hashmap entries  = new Hashmap<Integer, Integer>();
    for (int k = 0; k < arrNums2.length; k++) {
       if (!entries.containsKey(arrNums2[k])) {
         entries.put(arrNums2[k], 1);
       } else {
         // You already found the duplicate, so you can do whatever you like 
         Integer count = entries.get(arrNums2[k]);
         count = count + 1;
         entries.put(arrNums2[k], count);
       }
    }

#4


0  

This loops seems to go for ever until negative value limit reaches. It seems you have made the temp array arrNumsCompare to implement logic. Here you Go:

这个循环似乎会一直循环下去,直到负值极限达到。似乎您已经使临时数组arrNumsCompare来实现逻辑。给你:

   for (int k=0; k < arrNums2.length; k++) 
     {
       for (int i = 0; i < arrNums2.length; i++) 
       {
         if(i == k)
         {
           i++;
         } 
         if(arrNums2[k]==arrNums2[i])
         {             
            points = points -5;
            break;
         }
       }
     }

NOTE: If a value is present 3 times the deduction would be 10 and so on. If you want to deduct it once, you would need to work further.Ask me if you could not do that.

注意:如果一个值是3倍的现值,那么扣除将是10,以此类推。如果你想要减去它一次,你需要做进一步的工作。问我你能不能那样做。

Hint: you would need to maintain another array of number already marked duplicate and add another check to skip :)

提示:您需要维护另一个已标记为duplicate的数字数组,并添加另一个check to skip:)

#5


0  

Second Version to deduct points once for as many duplicates

第二种版本,每次扣除积分的次数相同

Array must contain only positive numbers for this solution.

数组必须只包含这个解决方案的正数。

for (int k=0; k < arrNums2.length; k++) 
     {
       if(arrNums2[k] == -1)
       {
         continue;
       }
       Boolean found = fasle;
       for (int i = k+1; i < arrNums2.length; i++) 
       {             
         if(arrNums2[k]==arrNums2[i])
         {  
            if(found == false)
            {            
               points = points -5;
               found = true;           
            }
            arrNums2[i] = -1;
         }
       }
     }

#6


0  

For the set {1,2,3,4,5,5,5,5,6,7,8,9,9,9,9} With points stating at 100 the following code will reduce the points by 10. As if only 2 duplicates exist. hope this helps.

对于集合{1、2、3、4、5、5、5、6、7、8、9、9、9、9},点在100,下面的代码将使点减少10。好像只有两个副本存在。希望这个有帮助。

private int[] numbers;
private Set<Integer> previousDuplicates = new HashSet<Integer>();
private int points;


public void reducePointsForDuplicates(){
    for (int i = 0; i < numbers.length; i++) {
         reducePointsIfDuplicate(i, numbers[i]);
    }
}

private void reducePointsIfDuplicate(int position, int number){
    for (int i = position + 1; i < numbers.length; i++) {
        if(number == numbers[i]){ 
            reducePointsForNewDuplicate(number);
        }
    }
}

private void reducePointsForNewDuplicate(int number){
    if (!previousDuplicates.contains(number)){
        points = points - 5;
    }
    previousDuplicates.add(number);
}



public int[] getNumbers() {
    return numbers;
}

public void setNumbers(int[] numbers) {
    this.numbers = numbers;
}

public int getPoints() {
    return points;
}

public void setPoints(int points) {
    this.points = points;
}

#1


4  

I suggest this one line solution :

我建议这一行解决方案:

Integer[] numbers = {1,2,3,4,5,6,5,4,3,2,1};

return 5 * (new HashSet<Integer>(Arrays.asList(numbers)).size() - numbers.length);

I add all the elements to a Set, which by definition refuses duplicates. Then I compare the size of the set to the length of the array and multiply the result by 5.

我将所有元素添加到一个集合中,该集合拒绝重复。然后我将集合的大小与数组的长度进行比较,并将结果乘以5。

#2


0  

You can work on this pseudo code:

你可以使用这个伪代码:

int nDuplicates = 0;
arrNums2 = arrNums2.Sort(); // Sort the array in ascending or descending order. 

for(int i =1; i<arrNums2.size(); ++i)
{ 
 if(arrNums2[i-1] == arrNums2[i])
 {
  nDuplicates++;
 }
}
Point -= nDuplicates*5;

Edit: If the input 5,5,5,5 has to be considered as a single duplication then use the following snippet.

编辑:如果必须将输入5、5、5视为单个重复,则使用以下代码片段。

    int nDuplicates = 0;
    arrNums2 = arrNums2.Sort(); // Sort the array in ascending or descending order. 
    int DuplicateNumber = 0;
       for(int i =1; i<arrNums2.size(); ++i)
        { 
         if(arrNums2[i-1] == arrNums2[i])
         {
          if(DuplicateNumber != arrNums2[i])
          {
           nDuplicates++;
           DuplicateNumber = arrNums2[i];
          }
         }
        }
     Point -= nDuplicates*5;

#3


0  

To find duplicates in one iteration over the original array without prior sorting you can use a HashMap to store each of the entries and increment count every time you find it again.
This way you can stop processing as soon as you find the first duplicate or continue to find all...

要在原始数组的一次迭代中找到重复,而不需要事先排序,您可以使用HashMap来存储每个条目,并在每次再次找到条目时增加计数。这样,您可以在找到第一个副本后立即停止处理,或者继续查找所有副本……

Edit: http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html

编辑:http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html

Hashmap entries  = new Hashmap<Integer, Integer>();
    for (int k = 0; k < arrNums2.length; k++) {
       if (!entries.containsKey(arrNums2[k])) {
         entries.put(arrNums2[k], 1);
       } else {
         // You already found the duplicate, so you can do whatever you like 
         Integer count = entries.get(arrNums2[k]);
         count = count + 1;
         entries.put(arrNums2[k], count);
       }
    }

#4


0  

This loops seems to go for ever until negative value limit reaches. It seems you have made the temp array arrNumsCompare to implement logic. Here you Go:

这个循环似乎会一直循环下去,直到负值极限达到。似乎您已经使临时数组arrNumsCompare来实现逻辑。给你:

   for (int k=0; k < arrNums2.length; k++) 
     {
       for (int i = 0; i < arrNums2.length; i++) 
       {
         if(i == k)
         {
           i++;
         } 
         if(arrNums2[k]==arrNums2[i])
         {             
            points = points -5;
            break;
         }
       }
     }

NOTE: If a value is present 3 times the deduction would be 10 and so on. If you want to deduct it once, you would need to work further.Ask me if you could not do that.

注意:如果一个值是3倍的现值,那么扣除将是10,以此类推。如果你想要减去它一次,你需要做进一步的工作。问我你能不能那样做。

Hint: you would need to maintain another array of number already marked duplicate and add another check to skip :)

提示:您需要维护另一个已标记为duplicate的数字数组,并添加另一个check to skip:)

#5


0  

Second Version to deduct points once for as many duplicates

第二种版本,每次扣除积分的次数相同

Array must contain only positive numbers for this solution.

数组必须只包含这个解决方案的正数。

for (int k=0; k < arrNums2.length; k++) 
     {
       if(arrNums2[k] == -1)
       {
         continue;
       }
       Boolean found = fasle;
       for (int i = k+1; i < arrNums2.length; i++) 
       {             
         if(arrNums2[k]==arrNums2[i])
         {  
            if(found == false)
            {            
               points = points -5;
               found = true;           
            }
            arrNums2[i] = -1;
         }
       }
     }

#6


0  

For the set {1,2,3,4,5,5,5,5,6,7,8,9,9,9,9} With points stating at 100 the following code will reduce the points by 10. As if only 2 duplicates exist. hope this helps.

对于集合{1、2、3、4、5、5、5、6、7、8、9、9、9、9},点在100,下面的代码将使点减少10。好像只有两个副本存在。希望这个有帮助。

private int[] numbers;
private Set<Integer> previousDuplicates = new HashSet<Integer>();
private int points;


public void reducePointsForDuplicates(){
    for (int i = 0; i < numbers.length; i++) {
         reducePointsIfDuplicate(i, numbers[i]);
    }
}

private void reducePointsIfDuplicate(int position, int number){
    for (int i = position + 1; i < numbers.length; i++) {
        if(number == numbers[i]){ 
            reducePointsForNewDuplicate(number);
        }
    }
}

private void reducePointsForNewDuplicate(int number){
    if (!previousDuplicates.contains(number)){
        points = points - 5;
    }
    previousDuplicates.add(number);
}



public int[] getNumbers() {
    return numbers;
}

public void setNumbers(int[] numbers) {
    this.numbers = numbers;
}

public int getPoints() {
    return points;
}

public void setPoints(int points) {
    this.points = points;
}