从阵列中删除重复项

时间:2016-03-06 22:45:31

标签: java arrays

我需要一些帮助,伙计们。 这是赋值:创建一个程序,从随机的整数数组中删除所有重复项。例如,如果数组具有值 4,7,10,4,9,5,10,7,3,5 然后应该将数组更改为 4,7,10,9,5,3

该程序应包含两个类DeleteDuplicate和DeleteDuplicateDemo。

DeleteDuplicate类应该执行以下操作 1.有一个检查重复值的方法。 2.跟踪阵列的电流大小 3.有一个删除重复项的方法(用数组中的下一个值替换重复值。)

DeleteDuplicateDemo类应该执行以下操作 1.有你的主要方法。 2.创建一个长度为10的数字的随机数组,范围从1-10。 3.显示原始随机数组。 4.调用DeleteDuplicate方法以查找和删除重复项。 5.显示没有重复的新数组。

示例输出: [8,5,7,3,2,5,6,3,6,7]

[8,5,7,3,2,6]

以下是DeleteDuplicateDemo的代码:

import java.util.*;

public class DeleteDuplicateDemo
{
   public static void main(String[] args)
   {

       Random random = new Random();
        int array[]= new int[10];
        for (int i = 0; i < array.length; i++) 
        {
            array[i] = random.nextInt(10)+ 1;
        }
        for (int i = 0; i < array.length; i++) 
        {        
            System.out.print(array[i] + " ");
        }
   System.out.println();
   DeleteDuplicate class1 = new DeleteDuplicate();
   array = class1.removeDuplicates(array);

 for (int i = 0; i < array.length; i++) 
        {        
            System.out.print(array[i] + " ");
        }

   }
}

以下是DeleteDuplicate的代码:

import java.util.Arrays;

public class DeleteDuplicate
{
   private static int[] remove(int[] array)
   {
      int current = array[0];
      boolean found = false;

      for (int i = 0; i < array.length; i++) 
      {
         if (current == array[i] && !found) 
         {
            found = true;
         } 
         else if (current != array[i]) 
         {
           System.out.print(" " + current);
           current = array[i];
           found = false;
         }
    }
       System.out.print(" " + current);
       return array;
   }

    public static int[] removeDuplicates(int[] array) {
      // Sorting array to bring duplicates together      
      Arrays.sort(array);

      int[] finalArray = new int[array.length];
      int previous = array[0];
      finalArray[0] = previous;

      for (int i = 1; i < array.length; i++){
         int value = array[i];

         if (previous != value){
            finalArray[i] = value;
         }
         previous = value;
      }
      return finalArray;

      }
}

我怎样才能让它发挥作用?我只是在调用另一个类时遇到问题。谢谢!

3 个答案:

答案 0 :(得分:0)

DeleteDuplicateDemo课程中,更改

DeleteDuplicate class1 = new DeleteDuplicate();
array = class1.removeDuplicates(array);

array = DeleteDuplicate.removeDuplicates(array);

DeleteDuplicate课程中,更改

for (int i = 1; i < array.length; i++){
    int value = array[i];
    if (previous != value){
        finalArray[i] = value;
    }
    previous = value;
}

for (int i = 1, j = 0; i < array.length; i++){
     int value = array[i];
     if (previous != value){
         finalArray[j] = value;
         j++;
     }
     previous = value;
}

然后,当您第二次将array的值打印到控制台时,请确保在打印array[i] != null之前检查是否array[i]

答案 1 :(得分:0)

删除代码太复杂了:而不是使用单独的方法删除项目,从索引1开始遍历数组,如果您编写的数据,则从读取索引复制到写入索引与索引i-1处的项目不同:

int rdIndex = 1;
int wrIndex = 1;
while (rdIndex != array.length) {
    if (array[wrIndex-1] != array[rdIndex]) {
        array[wrIndex++] = array[rdIndex];
    }
    rdIndex++;
}
return Arrays.copyOf(array, wrIndex);

在循环结束时wrIndex表示要复制到结果数组中的项目数。

Demo.

答案 2 :(得分:0)

试试这个。

public static int[] removeDuplicates(int[] array) {
    return IntStream.of(array).distinct().toArray();
}

试验:

int[] array = {8, 5, 7, 3, 2, 5, 6, 3, 6, 7};
System.out.println(Arrays.toString(removeDuplicates(array)));
// -> [8, 5, 7, 3, 2, 6]
相关问题