简体   繁体   English

消除数组中的重复项

[英]Eliminateing duplicates in an array

I have this professor that wants us to make a using the method header Public static int[] eliminateDuplicates(int[] arr) The program uses a randomly generated array to see if there are duplications but the user gives a limit on how many indexes's the random array has. 我有一位教授想让我们使用方法标头来做公共静态int [] excludeDuplicates(int [] arr)程序使用随机生成的数组来查看是否存在重复项,但是用户对多少个索引进行了限制有随机数组。 Here is what I have but it's not working. 这是我所拥有的,但不起作用。

import java.util.Random;
import java.util.Scanner;
public class Dublicates
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        Random generator = new Random();

        System.out.println("Enter array length: ");
        int[] a = new int[input.nextInt()];

        for (int i = 0; i<a.length; i++)
        {
            a[i] = generator.nextInt(a.length*2);
        }
        int[] result = eliminateDuplicates(a);
        System.out.println("The new numbers are: " + result.length);
        System.out.println("The double numbers were:");
        for (int b : result)
        {
            System.out.println(b + " ");
        }
    }
    public static int[] eliminateDuplicates(int[] arr)
    {
        int[] temp = new int[arr.length];
        int size = 0;
        for (int i = 0; i < arr.length; i++) {
            if (linearSearch(temp, arr[i]) == -1) {
                temp[size] = arr[i];
                size++;
            }
            int[] result = new int[size];
            for (int i = 0; i < size; i++) {
                result[i] = temp[i];
            }
            {
                return result;

            }
        }
    }
    public static int linearSearch(int[] arr, int key)
    {
        for(int i = 0; i<arr.length; i++)
        {
            if (key == arr[i])
                return i;
        }
        return -1;
    }
}

As your statements making the final result is inside the for loop, the statements inside for will only run once and will not give the right answer. 由于使最终结果的语句在for循环内,因此for内的语句仅运行一次,并且不会给出正确的答案。

So you have to change your code as follows. 因此,您必须按以下方式更改代码。

public static int[] eliminateDuplicates(int[] arr)
{
    int[] temp = new int[arr.length];
    int size = 0;
    for (int i = 0; i < arr.length; i++) {
        if (linearSearch(temp, arr[i]) == -1) {
            temp[size] = arr[i];
            size++;
        }
    }
    int[] result = new int[size];
    for (int i = 0; i < size; i++) {
        result[i] = temp[i];
    }

    return result;
}

The default values in the integer array is 0, The Random.nextInt() can generate 0 random value, When you run linear search then 0 will not be included in the final resultant array. 整数数组中的默认值为0,Random.nextInt()可以生成0个随机值,当您运行线性搜索时,最终的结果数组中将不包含0。 I have modify Random.nextInt() so that it will not generate 0 random number: 我修改了Random.nextInt(),使其不会生成0个随机数:

import java.util.Random;
import java.util.Scanner;
public class HelloCodiva
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        Random generator = new Random();

        System.out.println("Enter array length: ");
        int[] a = new int[input.nextInt()];      

        for (int i = 0; i<a.length; i++){
            a[i] = generator.nextInt(a.length*2)+1;//So that 0 is not generated
        }

        int[] result = eliminateDuplicates(a);

        for (int originalValue: a){
            System.out.println(originalValue+ " ");
        }

        System.out.println("The new numbers are: " + result.length);
        System.out.println("The double numbers were:");

        for (int b : result){
            System.out.println(b + " ");
        }
}

    public static int[] eliminateDuplicates(int[] arr)
    {
        int[] temp = new int[arr.length];
        int size = 0;
        for (int i = 0; i < arr.length; i++) {
            if (linearSearch(temp, arr[i]) == -1) {
                temp[size] = arr[i];
                size++;
            }
        }

        int[] result = new int[size];       
        System.arraycopy(temp, 0, result, 0, size);
       return result;
    }

    public static int linearSearch(int[] arr, int key)
    {
        for(int i = 0; i<arr.length; i++)
        {
            if (key == arr[i])
                return i;
        }
        return -1;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM