How to select duplicate values from a list in java

2019-02-22 18:15发布

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 case).

Is there any other efficient way rather than looping through list ? I asked this question because the list that I am working is very large ? My code is

List<Long> duplicate = new ArrayList();
for (int i = 0; i < list.size(); i++) {
     Long item = (Long) list.get(i);
     if (!duplicate.contains(item)) {
          duplicate.add(item);
         }
     }

10条回答
闹够了就滚
2楼-- · 2019-02-22 19:01

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

查看更多
萌系小妹纸
3楼-- · 2019-02-22 19:02

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.))
查看更多
三岁会撩人
4楼-- · 2019-02-22 19:03

Your List should ideally have been a Set which doesn't allow duplicates in the first place. As an alternative to looping, you could either convert and switch to Set or use it intermediately to eliminate duplicates as follows:

List<Long> dupesList = Arrays.asList(4L, 6L, 6L, 7L, 7L, 8L);

Set<Long> noDupesSet = new HashSet<Long>(dupesList);
System.out.println(noDupesSet); // prints: [4, 6, 7, 8]

// To convert back to List
Long[] noDupesArr = noDupesSet.toArray(new Long[noDupesSet.size()]);
List<Long> noDupesList = Arrays.asList(noDupesArr);
System.out.println(noDupesList); // prints: [4, 6, 7, 8]
查看更多
仙女界的扛把子
5楼-- · 2019-02-22 19:08

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.

查看更多
登录 后发表回答