-1

For example we have two ArrayList

ArrayList<Integer> first = new ArrayList<>();
ArrayList<Integer> second = new ArrayList<>();

And lets say we add some numbers to them:

first.add(1);
first.add(2);
second.add(2);
second.add(3);

And how to check here the condition like:

if(first.contains(second(element))){cnt++;(just increment hypothetical counter)}

Thanks

Andrei Kulik
  • 446
  • 1
  • 5
  • 21
  • I am trying to check if an element from first exists in another and opposite I want to count the number of integers that does not exists in another array(unique ones) – Bakhtovar Umarov May 05 '21 at 18:36
  • @BakhtovarUmarov So you want to know the number of elements from the first in the second? – Unmitigated May 05 '21 at 18:36
  • @Abra yes, but how to run a loop that would go through two arrays, I tried vide double for (i, j) but its not compiling – Bakhtovar Umarov May 05 '21 at 18:37
  • @iota I want to know the number of integer like this:(!first.contains(second.get(ELEMENT)) I want to find out which loop to use and how – Bakhtovar Umarov May 05 '21 at 18:39

2 Answers2

3

You can use Stream API to filter and count the elements in the second list.

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        ArrayList<Integer> first = new ArrayList<>();
        ArrayList<Integer> second = new ArrayList<>();

        first.add(1);
        first.add(2);
        second.add(2);
        second.add(3);
        second.add(2);

        first.forEach(n -> System.out
                .println(n + " exists " + second.stream().filter(e -> e == n).count() + " times in the second list"));
    }
}

Output:

1 exists 0 times in the second list
2 exists 2 times in the second list

Alternatively, you can use Collections#frequency to print the frequency of each number of the list, first in the list, second:

for (Integer x : first) {
    System.out.println(x + " exists " + Collections.frequency(second, x) + " times in the second list");
}

Alternatively, you can use the nested loops to iterate the list, second for each number in the list, first:

for (Integer x : first) {
    int count = 0;
    for (Integer y : second) {
        if (x == y)
            count++;
    }
    System.out.println(x + " exists " + count + " times in the second list");
}
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
0

Simpler version of code. I wasn't quite sure from the question what exactly you are looking for. So I have added code for two methods.

  1. One that returns count of unique elements which are not present in list 2 but available in list 1.

  2. The other method returns map with element and count of occurrence of that element present in both the lists.

    public static void main(String[] args) {
        
         ArrayList<Integer> first = new ArrayList<>();
         ArrayList<Integer> second = new ArrayList<>();

            first.add(1);
            first.add(2);
            first.add(2);
            first.add(2);
            first.add(2);
            first.add(4);
            second.add(2);
            second.add(3);
            second.add(2);
            second.add(1);
        
        int uniqueCount = getCountOfUniqueElements(first,second);
        Map<Integer,Integer> countMap = getCountMap(first,second);
        System.out.println("Count of unique Elements in First list that are not in second list="+uniqueCount);
        countMap.forEach((key,value) -> System.out.println(key + " occurred " + value + " times in first list"));

        
    }

    /**
     * Counts number of elements that are in first list but not in second list
     * @param first
     * @param second
     * @return count
     */
    private static int getCountOfUniqueElements(List<Integer> first,
            List<Integer> second) {
        
        if(first == null || first.isEmpty() || second == null || second.isEmpty()) //if lists are empty or null, count is 0
            return 0;
        
        Set<Integer> set = new HashSet<>(second); // Convert second list to set for faster search or constant time retrieval 
        int count = 0;
        for(Integer element : first) {
            if(!set.contains(element)) {
                count++;
            }
        }
        
        return count;
    }
    
    /**
     * Counts number of elements that are in first list which are also available in second 
     * list
     * @param first
     * @param second
     * @return map with key as first list element and value as number of its occurrences in first list
     */
    private static Map<Integer,Integer> getCountMap(List<Integer> first,
            List<Integer> second) {
        
        if(first == null || first.isEmpty() || second == null || second.isEmpty()) //if lists are empty or null, return emptyMap
            return Collections.emptyMap();
        
        Set<Integer> set = new HashSet<>(second); // Convert second list to set for faster search or constant time retrieval 
        Map<Integer,Integer> countMap = new HashMap<>(); // Maintains count of occurrences of first list element present in second list.
        for(Integer element : first) {
            if(set.contains(element)) {
                countMap.put(element, countMap.getOrDefault(element, 0)+1);
            }
        }
        
        return countMap;
    }