Java program to find missing numbers in an array?

  Рет қаралды 18,291

Learn With KrishnaSandeep

Learn With KrishnaSandeep

Күн бұрын

Пікірлер: 19
@krayush1109
@krayush1109 2 жыл бұрын
One of the best YT Channel to Learn Programming 🌟🌟🌟🌟🌟🌟🌟🌟🌟🌟🌟🌟🌟🌟🌟🌟🌟🌟🌟🌟🌟🌟
@svr9546
@svr9546 Жыл бұрын
just add Collections.. to findout Hashset Max and Min number import java.util.Collections; import java.util.HashSet; public class MissingNumberDemo { public static void main(String[] args) { Integer a[]= {2,4,1,5,7,8,12}; missingNumbers(a); } private static void missingNumbers(Integer[] a) { HashSet set = new HashSet(); for(int num:a) { set.add(num); } int max=Collections.max(set); int min=Collections.min(set); for(int i=min; i
@shariefrana
@shariefrana Жыл бұрын
Great solution
@suganyag1224
@suganyag1224 2 жыл бұрын
try to change any of the numbers. say make 2 as 12 in the array. The solution wouldn't work. we have to find the min and max element in the array and iterate. can use a treeset. import java.util.TreeSet; public class MissingNumbers { public static void main(String args[]) { Integer arr[] = {12,4,0,7,1,8,5}; missingNos(arr); } public static void missingNos(Integer a[]) { TreeSet set = new TreeSet(); for(int n : a) { set.add(n); } int small = set.first(); int large = set.last(); for(int i=small ; i
@sadhanasahu3623
@sadhanasahu3623 2 жыл бұрын
I was also thinking about this solution
@ankushgundale9692
@ankushgundale9692 Жыл бұрын
Yes you are correct this logic is not working if we change the numbers in the array....with your approach it worked fine for me as well...Dear author please add the same
@akshayshinde3869
@akshayshinde3869 Жыл бұрын
Good
@Sivasiva-ug4xc
@Sivasiva-ug4xc 3 ай бұрын
First you need find max element and store that to n it perfectly
@sasidharan4401
@sasidharan4401 2 жыл бұрын
Just assume if the int a[] ={2,8,3}..to find the missing numbers ..how to do this?
@sainath2983
@sainath2983 8 ай бұрын
public class Program { static void Main(string[] args) { int[] nums = {2,4,1,5,7,8,0 }; int n = nums.Max(); for(int i =0;i< n; i++) { if (!nums.Contains(i)) { Console.WriteLine(i); } } } }
@poojahani
@poojahani Жыл бұрын
Program wont work if we remove 3 elements in an array
@sivasankarkuna9079
@sivasankarkuna9079 Жыл бұрын
void printMissingNumbers(int[] nums) { Arrays.sort(nums); int counter =0; for (int i = 0; i < nums[nums.length-1];i++) { if (i==nums[counter]) { counter++; } else { System.out.println(i); } } }
@intrabull6696
@intrabull6696 2 жыл бұрын
nicee thankyou
@AkashVerma-em1gk
@AkashVerma-em1gk 10 ай бұрын
i would say, your code is not correct. check mine public static void main(String[] args) { // TODO Auto-generated method stub int[] arr = {0,1,2,3,4,5,10,15}; HashSet hs = new HashSet(); for(int i : arr) { hs.add(i); } Arrays.sort(arr); for(int i =0;i
@Karadkar007
@Karadkar007 2 жыл бұрын
can you explain why did you choose HashSet for this?
@intrabull6696
@intrabull6696 2 жыл бұрын
bcause default sorting nature order of HashSet is ascending when you remove the ! from contains() so you will get output 0124578 but when you use arraylist you will get 2,4,1,5,7,8,0 bcoz insertion order is preserved
@intrabull6696
@intrabull6696 2 жыл бұрын
but you can use anything bcoz your opretion is to find missing numbers so hashset not necessory
@dhairyasheelpawar2817
@dhairyasheelpawar2817 Жыл бұрын
Wrong program If we find missing number from {2,8}; Then it will not show all missing number between 2 and 8 wrong wrong wrong wrong
@samahmahdi5511
@samahmahdi5511 10 ай бұрын
import java.util.HashSet; import java.util.Set; public class MissingNumberDemo { public static void main(String[] args) { int a[]= {3,5,9,0,2}; Set list = missingNumbers(a); System.out.println(list); } public static Set missingNumbers(int[] num){ Set missingnumber = new HashSet(); Set list = new HashSet(num.length); for(int n : num){ list.add(n); } int max = num[0], min = num[0]; for(int i=1;i
How to find Missing Number In Array - Java Interview Question -4
12:40
Naveen AutomationLabs
Рет қаралды 105 М.
Find missing number in an array
8:31
Techdose
Рет қаралды 136 М.
The evil clown plays a prank on the angel
00:39
超人夫妇
Рет қаралды 53 МЛН
黑天使被操控了#short #angel #clown
00:40
Super Beauty team
Рет қаралды 61 МЛН
BAYGUYSTAN | 1 СЕРИЯ | bayGUYS
36:55
bayGUYS
Рет қаралды 1,9 МЛН
Java program to find majority element in an array?
18:10
Learn With KrishnaSandeep
Рет қаралды 22 М.
AI Is Making You An Illiterate Programmer
27:22
ThePrimeTime
Рет қаралды 231 М.
Write a java program to sort names in an array?
19:32
Learn With KrishnaSandeep
Рет қаралды 40 М.
The evil clown plays a prank on the angel
00:39
超人夫妇
Рет қаралды 53 МЛН