Find common element between two array

0 votes
Can someone help me with finding common element between two array in java? Thank you in advance.
Mar 11, 2019 in Java by Suresh
1,780 views

1 answer to this question.

0 votes

You just need nested loop, that will surve the purpose,

public class HelloWorld {
   public static void main(String a[]) {
      int[] arr1 = {4,7,3,9,2};
      int[] arr2 = {3,2,12,9,40,32,4};      
      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]);
            } 
         } 
      }
   }
}
answered Mar 11, 2019 by Esha Gupta

Related Questions In Java

0 votes
1 answer

How to calculate the difference between two date instances in Java?

You can use Joda Time Library. Interval i ...READ MORE

answered May 4, 2018 in Java by Parth
• 4,640 points
919 views
0 votes
2 answers

How can we remove an element from an array in Java?

You can use ArrayUtils class remove method which ...READ MORE

answered May 24, 2018 in Java by UshaK
2,681 views
0 votes
1 answer

In Java, how to find out the min/max values from array of primitive data types?

import java.util.Arrays; import java.util.Collections; import org.apache.commons.lang.ArrayUtils; public class MinMaxValue { ...READ MORE

answered Jun 12, 2018 in Java by Daisy
• 8,140 points
1,608 views
0 votes
1 answer

How can I calculate number of days between two dates?

You may refer this. This might work ...READ MORE

answered Jul 19, 2018 in Java by Akrati
• 3,190 points
1,201 views
+1 vote
1 answer

Are arrays equivalent to objects in Java ?

Yes; the Java Language Specification writes: In the Java ...READ MORE

answered May 10, 2018 in Java by Rishabh
• 3,620 points
1,258 views
+1 vote
1 answer

Remove objects from an array in Java?

We can use external libraries: org.apache.commons.lang.ArrayUtils.remove(java.lang.Object[] array, int ...READ MORE

answered Jun 26, 2018 in Java by scarlett
• 1,290 points
1,150 views
+1 vote
3 answers

What is the syntax to declare and initialize an array in java?

You can use this method: String[] strs = ...READ MORE

answered Jul 25, 2018 in Java by samarth295
• 2,220 points
3,470 views
0 votes
2 answers

What is the syntax to initialize an array?

Rather than learning un-Official websites learn from ...READ MORE

answered Aug 2, 2018 in Java by samarth295
• 2,220 points
911 views
0 votes
1 answer

Merge two array in Java

This is very simple, you need an ...READ MORE

answered Mar 11, 2019 in Java by Esha Gupta
1,066 views
0 votes
2 answers

Reverse an Array in Java

If you want to reverse the array ...READ MORE

answered Aug 28, 2019 in Java by Sirajul
• 59,230 points
737 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP