繁体   English   中英

如何从输入数组中删除重复项并将其放入输出列表中?

[英]How to remove duplicates from input array and put them in output list?

public class Exercise1 {
//Notice that in this main, each of the functions have been called for you
public static void main(String[] args) {
    int[] data = {1,3,5,4,7,9,1,3};
    int[] output = new int[data.length];

    System.out.println("Does our array contain a '1':"+ contains(data, 1)); //true
    System.out.println("Does our array contain a '0':"+ contains(data, 0)); //false
    System.out.println("What is the index of '4'? " + indexOf(data, 4)); //3
    System.out.println("The number of occurrences of '1'? " + count(data, 1)); //2

    duplicates(data, output);
    System.out.println("After removing duplicates from data:");
    System.out.println(Arrays.toString(output));
}

public static boolean contains(int[] input, int target) {
    //todo: see lab
    for (int i = 0; i < input.length; i++) {
        if (target == input[i])
            return true;
    }
    return false;
}

public static int indexOf(int[] input, int target) {
    //todo: only find the indexOf a target if we contain() it
    if (contains(input, target) == false) {
        return -1;
    }

    for (int i = 0; i < input.length; i++) {
        if (target == input[i]) {
            return i;
        }
    }
    return -1;
}

public static int count(int[] input, int target){
    int retVal = 0;
    //todo: only try to count a number that we contain()
    if (contains(input, target) == true) {
        for (int i = 0; i < input.length; i++) {
            if (input[i] == target) {
                retVal++;
            }
            else {
                retVal = retVal;
            }
        }
    }
    return retVal;
}

public static void duplicates(int[] input, int[] output) {
    //todo: Transfer items once from the arrays:input and output.
    //transfer items from input to the output array IF:  
    //only if newArray.count(target) == 0 //ie, we haven't put this in yet
    //only if newArray.indexOf(target) == -1 //not found in newArray, or
    //only if newArray.contains(target) == false //does not exist in the new array

}

}

除了重复项之外,我已经使所有方法都可以使用-我必须从主数组中删除重复项,以便它应输出{1、3、5、4、7、9、0、0}。 为此,我们应该调用count()方法,但是如果参数不同,我不确定如何重复调用该方法。 有人可以帮忙吗?

对于input数组中的每个item :调用count(input,item) 然后,如果结果大于1,则它是重复项。 因此,将其从input删除并放到output

不要编写代码来标识重复项,而是使它们相似,然后仅删除重复项。

我只是在我的本地尝试过,并且有效。

package tes;

import java.util.Arrays;

public class RemoveDuplicatesfromArray {

    // setup code
    public static void main(String[] args) {

        int[] source = { 1, 3, 5, 4, 7, 9, 1, 3 };
        int[] target = Arrays.copyOf(source, source.length);
        removeDuplicates(source, target);
        printarray(source, target);
    }

    // helper not needed just to print and check 
    private static void printarray(int[] source, int[] target) {

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

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

    // actual logic
    public static void removeDuplicates(int[] source, int[] target) {

        for (int i = 0; i < source.length - 1; i++) {

            for (int j = i + 1; j < source.length; j++) {

                if (source[i] == source[j]) {
                    target[j] = 0;
                }
            }
        }

    }

}

HTH ..

public static void duplicates(int[] input, int[] output){
    for (int q = 0; q < input.length; q++){
        if (!contains(output, input[q]) || 
                indexOf(output, input[q]) == -1 || 
                count(output, input[q]) == 0){ // # of times the current input appears in 
                // output should be zero
            output[q] = input[q];
        }
    }
}

暂无
暂无

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

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