Compare two arrays for the same value

// duplicate elements in both arrays
String[] str1={1,55,22,55,41,21,12,8,9};
String[] str2={1,33,23,52,41,22,12,2,5,8};

Arrays.sort(str1);
Arrays.sort(str2);

if(Arrays.equals(str1, str2)){
   System.out.print("Repeat element");
}
List<String> list=new ArrayList<>();
List<String> list2=new ArrayList<>();
   
list.addAll(Arrays.asList(str1));
list2.addAll(Arrays.asList(str2);

//retainAll will remove elements that do not exist in both arrays
if(list.retainAll(list2)){
    System.out.print("Repeat element");
}

//whether a value exists in the array
if(Arrays.asList(str1).contains("1")){
   System.out.print("There is an element");
}

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326871761&siteId=291194637