Java – removes duplicate public output from both lists

I have two arrays I want the output to be a common number between two lists, but the following code will output duplicate common numbers

double[] arr1 = {100.00,100.00,17246.40,2568.00,0.20,9845.00,5768.18,30.00,63.68,83.56,444.39,144.20,2889.00};

 double[] arr2 = {2000.90,508.07,5899.38,2889.00,3000.00,60.00,3135.00,28329.91,9845.00};

 for(int i=0; i<arr1.length; i++){
      for(int j=0; j<arr2.length; j++){
           if(arr1[i] == arr2[j]){
                System.out.println(arr1[i]);

Can anyone help output only public numbers between two arrays and delete duplicate numbers?

thank you.

Solution

To create a collection, simply add common values

double[] arr1 = {100.00,2889.00};


double[] arr2 = {2000.90,9845.00};

Set<Double> set = new HashSet<Double>();

for(int i=0;i<arr1.length;i++){

     for(int j=0;j<arr2.length;j++){
           if(arr1[i]==arr2[j])
                set.add(arr1[i]);
     }
}

System.out.println(Arrays.toString(set.toArray()));
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>