简体   繁体   中英

How can I print out this array method that finds duplicates?

package Practice3;

import java.util.Arrays;
import java.util.Scanner;

public class ray1 {

    public static int[] findDuplicates(int[] userInput)
    {
        for(int i = 0; i< userInput.length; i++)
        {
            for (int j = i+1; j < userInput.length; j++)
            {
                if(userInput[i]==(userInput[j]))
                {
                    System.out.println(userInput[i]);
                }
            }
        }
        return userInput;
    }

    public static void main(String[] args) 
    {
        int[] listOfNumbers = { 1, 2, 3, 4, 1, 2, 3, 1 };

        int[] x = findDuplicates(listOfNumbers);
        Arrays.sort(x);
        System.out.println(Arrays.toString(x));
    }
}

I think the issue could be in the static void main, but I'm not quite sure what's wrong with the code.

You return the input you got so of course you wont get the duplicates. You should create a hashset in your function and add the duplicates to it.

  public static void main(String[] args) {
        int[] listOfNumbers = { 1, 2, 3, 4, 1, 2, 3, 1 };
        ArrayList<Integer> list = new ArrayList<>(findDuplicates(listOfNumbers));
        Collections.sort(list);
        System.out.println(list);
  }
  
  
      public static HashSet<Integer> findDuplicates(int[] userInput) {
        HashSet<Integer> output = new HashSet<Integer>();
        for(int i = 0; i< userInput.length; i++) {
            for (int j = i+1; j < userInput.length; j++) {
                if(userInput[i]== userInput[j]) {
                    output.add(userInput[i]);
                }
            }
        }
        return output;
    }

Notice that I used return type as HashSet, it's much more appropriate as you want each duplicate to appear once and then I converted it to an ArrayList and printed it.

As per your code, both int[] listOfNumbers and int[] x are the same. You have passed int[] listOfNumbers into the findDuplicates() method, and returned the same without changing anything.

If you are trying to remove the duplicates inside that method, then you need to do something inside the loop where you check for value[i] == value[j] where i!=j .

One suggestion for you is to try and add the numbers into a Collection when value[i] != value[j] , but not when they are similar.

When this is done, at the end of the two loops you will have a Collection with unique numbers. Then you will be able to sort and print.

Note: This is not the simplest or the optimal way as Java has a concept called Set to collect unique elements. First, it is good if you can change your method and figure out how to get these two loops to cater the requirement.

I'd suggest first fixing the findDuplicates method to do what it's supposed to do. My understanding is that it should return an array that contains the values that occur more than once in the userInput array.

One way to to this could be to create a new array at the beginning of the findDuplicates method and name it for example duplicatedValues . You can then fill it with the values that the rest of the method logic identifies as duplicates. Given your statement in one of your comments that the output should be [ 1, 2, 3 ] , I think you should also alter the if statement to only accept values that are not already in the duplicatedValues array, which might even mean a 3rd for loop. Otherwise, the duplicatedValues array will itself also contain duplicates.

When you've populated the duplicatedValues array with "unique duplicates", you can return that array instead of the userInput array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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