How to count duplicate elements in ArrayList?

前端 未结 10 1561
悲哀的现实
悲哀的现实 2020-12-15 11:55

I need to separate and count how many values in arraylist are the same and print them according to the number of occurrences.

I\'ve got an arraylist called digits :<

相关标签:
10条回答
  • 2020-12-15 12:07

    Java 8 can handle this problem with 3 lines of code.

        Map<Integer, Integer>  duplicatedCount = new LinkedHashMap<>();
        list.forEach(a -> duplicatedCount.put(a, duplicatedCount.getOrDefault(a, 0) +1));
        duplicatedCount.forEach((k,v) -> System.out.println(v+" times "+k));
    
    0 讨论(0)
  • 2020-12-15 12:09

    Map#merge method can also be used:

     List<Integer> input = Arrays.asList(1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584,
                4181, 6765);
     final Map<Integer, Integer> result = new HashMap<>();
     input.forEach(in -> result.merge(in, 1, Integer::sum));
    
    0 讨论(0)
  • 2020-12-15 12:14

    By using the Stream API for example.

    package tests;
    
    import org.junit.Assert;
    import org.junit.Test;
    
    import java.util.Arrays;
    import java.util.List;
    import java.util.Map;
    import java.util.function.Function;
    import java.util.stream.Collectors;
    
    public class Duplicates {
    
        @Test
        public void duplicates() throws Exception {
            List<Integer> items = Arrays.asList(1, 1, 2, 2, 2, 2);
    
            Map<Integer, Long> result = items.stream()
                    .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
    
            Assert.assertEquals(Long.valueOf(2), result.get(1));
            Assert.assertEquals(Long.valueOf(4), result.get(2));
        }
    }
    
    0 讨论(0)
  • 2020-12-15 12:18

    use Collections.frequency method to count the duplicates

    0 讨论(0)
  • 2020-12-15 12:18

    Well, for that you can try to use Map

    Map<Integer, Integer> countMap = new HashMap<>();
    
      for (Integer item: yourArrayList) {
    
          if (countMap.containsKey(item))
              countMap.put(item, countMap.get(item) + 1);
          else
              countMap.put(item, 1);
      }
    

    After end of forEach loop you will have a filled map with your items against it count

    0 讨论(0)
  • 2020-12-15 12:19

    You can count the number of duplicate elements in a list by adding all the elements of the list and storing it in a hashset, once that is done, all you need to know is get the difference in the size of the hashset and the list.

    ArrayList<String> al = new ArrayList<String>();
    al.add("Santosh");
    al.add("Saket");
    al.add("Saket");
    al.add("Shyam");
    al.add("Santosh");
    al.add("Shyam");
    al.add("Santosh");
    al.add("Santosh");
    HashSet<String> hs = new HashSet<String>();
    hs.addAll(al);
    int totalDuplicates =al.size() - hs.size();
    System.out.println(totalDuplicates);
    

    Let me know if this needs more clarification

    0 讨论(0)
提交回复
热议问题