Find Intersection of Two Arrays – Java Code
Video Title: Find Intersection of Two Arrays – Java Code
Hello friend on this tutorial we are going to solve another interesting problem fine intersection of two area suppose we have two sorted error given two sorted array we have to write a Java code to find intersection of two error for example if the input arrays are array one and added to the common
Element intersection is simply the common element between these two way so the common element these two array is six and nine so our output should be six and nine so there are multiple ways to solve this problem so the first approach and the most simplest one is we take
Each element of the first array and compare with each element of the second area but the time complexity of this approaches oh and where m and n is the number of element present array 1 and array – so for demonstration purpose I am going to write a code for this
Approach we have int 0 now I wait one month our process for second era in which J is 0 J is less than a to 1 and J plus s if the element of first array equal to equal to element of second array then let’s
Volume and if I run this code we get the desire out but definitely this is not the best solution of this problem what if we have to traverse we draw us both the array simultaneously by maintaining or two indexes and find the common element between these two way then our
Time complexity should be reduced to Oh M plus n which is much better than our first approach where the time complexity was oh and so now let’s solve this problem using another approach and taking two variable I and J and assigning their initial values with 0 no
I run a while loop where I compare I with wave 1 and J with added 2 dot length so these condition are important as both array should either of equal or not I mean it’s not mandatory that’s both the two arrays are of equal length so that’s why this condition is very
Important if first array element of first array equal to element of second array then let’s print the element and increment ing else if the value of reform is greater than Eric – then increment G as we have sorted array so if the value of everyone I mean everyone
Is greater than aratoon then we have to increment the index of era – as we are going to find the common element between those arrow else increment the index off i if i run this blue if I run this program C we get the desired result 6 and 9 so let’s try run
This code so we have everyone an array – so and the initial value of I and J is 0 so if I start from beggining I is less than 5 1 2 3 4 5 6 so the value of array 1 is 6 and everyone is 6 and the length
Of array 106 and the length of arrow 2 is 5 so J is less than 5 so that value of I and J is 0 so this condition is true the value of first array at 0th index is equal to the second array excellent next no so this code will not
Execute the value of everyone like this index is greater than this no so else we increment the count of I so the value of is one here and the value of J is still zero so we are checking the value present at array one is equal to four know the value now the
Second condition where we are checking 3 is greater than 4 no so again is incremented and the condition become so it is we reach at second index and here we have 0 at element so 4 is equal to equal to 6 more so when this code is no second condition
Array I but I mean the value of array everyone at this index is greater than the value of era two at this index yes so J is incremented now this code will not execute it and so the the next condition we get is this so the value of
Everyone at this index is equal to this now we found our first common element between between between these two arrays and we incremented the count of I and J so our here is 3 and here is 2 so the value of at this index
I mean 7 is equal to 8 know the value 7 is greater than 8 this condition no so we increment it the value of I know it is 4 and it is still to the value at this index is equal to this know the value of array I first index I is
Greater than this yes so the value of J is incremental so this is equal to this yes we found our second common element within these two arrays and our logic goes like this so this is much better approach then the first one to find common element between
Two where is I and Nick our next approach is to use hash said so let’s remove this code what if I declare hash set of integer such one and traverse the first element and put it it now said no effect hours the second album second array and check
If set one contains this value if it contains then print print the value as we find found the common element if I run this code see we get our desire output which is six and nine so what I have done is I have traverse the first array and put each element of first
Array in a set and now in this step I Traverse a second second array and check each element whether it contains in first in this set if it is then we found our common element between these two array if not then we won’t print anything so I hope you have understood
These three approaches and thanks for watching this video I have I will mention the code link in the description box please like our video subscribe our channel for more such video and thanks for watching this video
-
Sale!
Wireless WIFI Repeater Extender Amplifier Booster 300Mbps
$29.99$14.99 Add to cartWireless WIFI Repeater Extender Amplifier Booster 300Mbps
Categories: Electronics, Wi-Fi Router, Wireless Wi-Fi Extender Tags: 300Mbps, 802.11N, Amplifier, Booster, Extender, mobile wi-fi booster, Remote, WIFI, Wireless, Wireless WIFI, Wireless WIFI Repeater, Wireless WIFI Repeater Extender, Wireless WIFI Repeater Extender Amplifier, Wireless WIFI Repeater Extender Amplifier Booster, Wireless WIFI Repeater Extender Amplifier Booster 300Mbps$29.99$14.99 -
Sale!
Full RGB Light Design Gaming Headset Headphones with Mic
$24.99$14.99 Add to cartFull RGB Light Design Gaming Headset Headphones with Mic
Categories: Electronics, Gaming, Gaming Headsets Tags: Design, Full, Full RGB Light Design Gaming Headset, Full RGB Light Design Gaming Headset Headphones, Full RGB Light Design Gaming Headset Headphones with Mic, Gamer, Gaming, Gaming Headset Headphones, gaming headset wireless, Headphone, Headphones, Headset, Light, Mic, Package, RGB$24.99$14.99 -
Sale!
Wireless BlueTooth Multi-Device Keyboard Mouse Combo
$39.99$19.99 Add to cartWireless BlueTooth Multi-Device Keyboard Mouse Combo
Categories: Electronics, Gaming, Gaming Keyboards, Keyboard Mouse Combos Tags: Combo, Keyboard, keyboard mouse combos, Mouse, MultiDevice, Set, WireKeyboard Mouse Combo, Wireless, Wireless BlueTooth Keyboard Mouse Combo, Wireless BlueTooth Keyboard Mouse Combos, Wireless BlueTooth Multi-Device Keyboard Mouse Combo, Wireless BlueTooth Multi-Device Keyboard Mouse Combos$39.99$19.99 -
Sale!
High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
$199.99$139.99 Add to cartHigh Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
Categories: Gaming, Gaming Chairs Tags: Adjustable, Chair, computer chairs, Desk, Executive, Gaming, Girl, Headrest, High, High Back Leather Executive Adjustable Swivel Gaming Chair, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar, High Back Leather Executive Adjustable Swivel Gaming Chairs, Leather, Lumbar, Office, Racing, Swivel$199.99$139.99 -
Sale!
Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
$29.99$19.99 Select optionsProfessional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
SKU: N/A Categories: Electronics, Gaming, Gaming Headsets Tags: Cancelling, Gaming, Gaming Headphones with Noise Cancelling Microphone, gaming headset, Headphones, Headset, LED, Light, Mic, Microphone, Noise, Professional, Professional LED Light Wired Gaming Headphones, Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone, Wired, Wired Gaming Headphones, Wired Gaming Headphones with Noise Cancelling Microphone$29.99$19.99 -
Sale!
Gaming Desk with LED Lights USB Power Outlets and Charging Ports
$349.99$249.99 Select optionsGaming Desk with LED Lights USB Power Outlets and Charging Ports
SKU: N/A Categories: Computer Desk, Gaming, Gaming Desk Tags: and Charging Ports, Charging, Desk, Desks, Gaming, gaming desk with led lights, Gaming Desks with LED Lights, Home, LED, Lights, Monitor, Office, Outlets, Port, Power, Room, Stand, USB, USB Power Outlets, White, Workstation$349.99$249.99 -
Sale!
Wired Mixed Backlit Anti-Ghosting Gaming Keyboard
$99.99$79.99 Add to cartWired Mixed Backlit Anti-Ghosting Gaming Keyboard
Categories: Electronics, Gaming, Gaming Keyboards Tags: Antighosting, Backlit, Blue, brown, Gaming, Gaming Keyboard, gaming keyboards, gaming keyboards and mouse, Keyboard, Laptop, Switch, Wired, Wired Mixed Backlit Anti-Ghosting Gaming Keyboard, Wired Mixed Backlit Anti-Ghosting Gaming Keyboards, Wired Mixed Backlit Gaming Keyboard$99.99$79.99 -
Sale!
Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
$119.99$59.99 Add to cartWireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
Categories: Electronics, Gaming, Gaming Headsets Tags: 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, ANC, Audio, Bluetooth, Cancellation, Ear, Earphone, gaming headset, Headphones, Headset, Hi-Res Over the Ear Headphones Headset, HiRes, Noise, Wireless, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Headphones, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headsets$119.99$59.99 -
Sale!
Wired Sports Gaming Headset Earbuds with Microphone
$19.99$9.99 Select optionsWired Sports Gaming Headset Earbuds with Microphone
SKU: N/A Categories: Gaming, Gaming Headsets Tags: Accessories, Earbud, Earphone, Earphones, Gaming, gaming headset with microphone, Headphones, Headset, IOS, Microphone, Sports, Wired, Wired Sports Gaming Headset Earbuds, Wired Sports Gaming Headset Earbuds with Microphone, Wired Sports Headset Earbuds$19.99$9.99 -
Sale!
150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
$49.99$29.99 Add to cart150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
Categories: Charging Stations, Electronics Tags: 150W, 150W Charging Station, 150W Universal Multi USB Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Stations, 150W Universal Multi USB MAX Charging Station, 16 Port MAX Charging Station, 3.5A, Charger, Charging, Fast, laptop charging stations, Max, Multi, Port, Stand, Station, Universal, USB$49.99$29.99
very well explained.
Using HashSet is very smart here
What if array is not sorted.. Then which approach will you use
This program will not pass all cases it consists of so many bugs
8:44 HashSet
Nice video but one minor correction: You should state explicitly that your first O(m+n) solution works ONLY for sorted arrays. The failing test case is [6,1,2,3] and [1,2,3]. Your second O(m+n) solution works for both types of arrays obviously.
how do we ignore repeated number for eg arr1=[1,2,3,4,5] arr2=[1,1,2,2,3] output= 1,1,2,2 but desired output =[1,2]
What if the 2nd array contains 2 values which are same. It would be printed 2 times which is wrong
same as two array of tunion example can you explain sir
You're finding common elements, this is not intersection.
Can u pls tell me the platform u r using sir or the software
Code link – https://webrewrite.com/find-intersection-two-arrays/
Code link is present in the description box.
The way you have explained two approaches to solve this problem is awesome.