How to select duplicate values from a list in java?

后端 未结 13 828
傲寒
傲寒 2021-01-18 00:38

For example my list contains {4, 6, 6, 7, 7, 8} and I want final result = {6, 6, 7, 7}

One way is to loop through the list and eliminate unique values (4, 8 in this

相关标签:
13条回答
  • 2021-01-18 01:10

    Here's my version of the solution:

    import java.util.ArrayList;
    
    public class Main {
    
    public static void main(String[] args) {
    
        ArrayList<Integer> randomNumbers = new ArrayList<Integer>();
        ArrayList<Integer> expandingPlace = new ArrayList<Integer>();
        ArrayList<Integer> sequenceOfDuplicates = new ArrayList<Integer>();
    
        for (int i = 0; i < 100; i++) {
            randomNumbers.add((int) (Math.random() * 10));
            expandingPlace.add(randomNumbers.get(i));
        }
    
        System.out.println(randomNumbers); // Original list.
    
        for (int i = 0; i < randomNumbers.size(); i++) {
            if (expandingPlace.get(i) == expandingPlace.get(i + 1)) {
                expandingPlace.add(0);
                sequenceOfDuplicates.add(expandingPlace.get(i)); 
                sequenceOfDuplicates.add(expandingPlace.get(i + 1));
            }
        }
    
        System.out.println(sequenceOfDuplicates); // What was in duplicate there.
    
    }
    
    }
    

    It adds numbers from 0 to 9 to a list, and it adds to another list what is in "duplicate" (a number followed by the same number). You can use your big list instead of my randomNumbers ArrayList.

    0 讨论(0)
  • 2021-01-18 01:11

    I like answer Java 8, Streams to find the duplicate elements. Solution return only unique duplicates.

     Integer[] numbers = new Integer[] { 1, 2, 1, 3, 4, 4 };
     Set<Integer> allItems = new HashSet<>();
     Set<Integer> duplicates = Arrays.stream(numbers)
        .filter(n -> !allItems.add(n)) //Set.add() returns false if the item was already in the set.
        .collect(Collectors.toSet());
     System.out.println(duplicates); // [1, 4]
    
    0 讨论(0)
  • 2021-01-18 01:14

    Is there any other efficient way rather than looping through list ?

    You could hire a magic elf and let it do it for you. How would you ever want to do this without looping through it? If you don't loop through the list, you even won't be able to have a look at the elements. It is like you want to sum a whole bunch of numbers together without looking at those numbers. Summing elements is much easier than searching for duplicates or searching for unique elements. In general, 97% of what code does is looping through lists and data and process and update it.

    So, said that, you have to loop. Now you might want to choose the most efficient way. Some methods come to mind:

    • Sort all the numbers and then loop only once through it to find duplicates (since they will be next to each other). However, keep in mind that sorting algorithms also loop through the data.
    • For each element in the list, check if there is another element with the same value. (This is how you did it. This means you have two loops inside each other. (contains loops through the list of course.))
    0 讨论(0)
  • 2021-01-18 01:18

    Given that you can do this by looping through the list only once, I wouldn't worry about performance too much. If you search for more performant solutions then you'll probably end up over-complicating the code and the readability and maintainability will suffer. At the end of the day, if you want to check the whole list for duplicates then you have to visit every element.

    I'd advise writing the obvious solution and see how it performs. You'll probably be surprised how fast Java can iterate over a list, even if it is particularly large.

    0 讨论(0)
  • 2021-01-18 01:23

    Have a

    Map<Integer, Integer> numberToOccurance = new HashMap<Integer, Integer>();
    

    maintain count and number, at the end iterate keyset and get values with more than one count

    0 讨论(0)
  • 2021-01-18 01:25

    Try this:

    Inspired by this answer: https://stackoverflow.com/a/41262509/11256849

    for (String s : yourList){
         if (indexOfNth(yourList, s, 2) != -1){
             Log.d(TAG, s);
          }
       }
    

    Using this method:

    public static <T> int indexOfNth(ArrayList list, T find, int nthOccurrence) {
            if (list == null || list.isEmpty()) return -1;
            int hitCount = 0;
            for (int index = 0; index < list.size(); index++) {
                if (list.get(index).equals(find)) {
                    hitCount++;
                }
                if (hitCount == nthOccurrence) return index;
            }
            return -1;
        }
    
    0 讨论(0)
提交回复
热议问题