简体   繁体   中英

How to check if an element from an array is being repeated in another array. All these arrays are inside of a Multidimensional array?

basically I'm looping through each element from each array trying to find an element that is also an element from another array, and if there's an element that is being repeated in another array I want to print out that element as well as STOPING the loop . In simply words this is what I have:

def list = [[2,3,5,10,13], [12,23,9,8], [34,11,14,15,67,28,5], [7,23,67,27,30,33]]

IMPORTANT : An element will never show up two times in the same array

I need to loop through the elements of each array and compare it to the other elements from other arrays, and if there's an element that is being repeated (Example: 5 - this number is being repeated in array1 and array3) then my loop should STOP. I've being stuck on this for a while. Does anyone know how to solve this in Groovy please? Thanks a lot in advance!

def list = [
    [2,3,5,10,13], 
    [12,23,9,8], 
    [34,11,14,15,67,28,5], 
    [7,23,67,27,30,33]
]

list.flatten().countBy { it }.findResult { k, v -> v > 1 ? k : null }

Declare Bidimensional-Array

 Integer [][]a = {{2,3,5,10,13}, {12,23,9,8}, {34,5,11,14,15}, {7,23,67,27,30,33}};

Looking for the number with intersection between Sets

boolean numberFound = false;
int number=0;
for (int i = 0; i < a.length && !numberFound; i++){
    for (int j = i+1; j < a.length && !numberFound; j++) {
        HashSet<Integer> intersection = new HashSet<Integer>(Arrays.asList(a[i]));
        intersection.retainAll(Arrays.asList(a[j]));
        if(intersection.size()>0){
            numberFound = true;
            number = intersection.iterator().next().intValue();
        }
    }
}

Printing

if(numberFound){
    System.out.println("Number found is: " +number);
} else{
    System.out.println("Number not found");
}

UPDATE

If we are sure that an element will never show up two times in the same array we can use this code:

Entry<Integer, Long> entry = Arrays.stream(a)
 .flatMapToInt(Arrays::stream)
   .boxed()
     .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
       .entrySet()
        .stream()
         .filter(s -> s.getValue() > 1)
          .findAny()
           .orElse(null);

if(entry == null){
 System.out.println("Number not found");
}else {
 System.out.println("Number found: " + orElse.getKey());
}
int[][] list = {{2,3,5,10,13}, {12,23,9,8}, {34,11,14,15,67,28,5}, {7,23,67,27,30,33}};
HashSet<Integer> seenItems = new HashSet<>();
for(int i = 0; i < list.length; i++) {
    int[] l = list[i];
    for(int j = 0; j < l.length; j++) {
        int itemToCheck = l[j];
        if (seenItems.contains(itemToCheck)) {
            System.out.println("We've already seen " + itemToCheck);
            return;
        } else {
            seenItems.add(itemToCheck);
        }
    }
 }

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