Java program to find missing numbers in an array?

preview_player
Показать описание

Рекомендации по теме
Комментарии
Автор

One of the best YT Channel to Learn Programming 🌟🌟🌟🌟🌟🌟🌟🌟🌟🌟🌟🌟🌟🌟🌟🌟🌟🌟🌟🌟🌟🌟

krayush
Автор

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<Integer> set = new HashSet<Integer>();

for(int num:a) {
set.add(num);
}
int max=Collections.max(set);
int min=Collections.min(set);

for(int i=min; i<max; i++) {
if(!set.contains(i)) {
System.out.println(i);
}
}
}

}
same program except 2 lines change

svr
Автор

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<Integer> set = new TreeSet<Integer>();

for(int n : a) {
set.add(n);
}
int small = set.first();
int large = set.last();

for(int i=small ; i<= large; i++) {
if(!set.contains(i)) {
System.out.println(i);
}
}

}

}

suganyag
Автор

First you need find max element and store that to n it perfectly

Sivasiva-ugxc
Автор

Just assume if the int a[] ={2, 8, 3}..to find the missing numbers ..how to do this?

sasidharan
Автор

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);
}

}
}

sivasankarkuna
Автор

Program wont work if we remove 3 elements in an array

poojahani
Автор

can you explain why did you choose HashSet for this?

Karadkar
Автор

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);
}
}
}
}

sainath
Автор

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<Integer> hs = new HashSet<>();
for(int i : arr) { hs.add(i); }

Arrays.sort(arr);
for(int i =0;i<arr[arr.length-1];i++)
{
if(!hs.contains(i))
{
System.out.print(i);

}

}


}
output: 6
7
8
9
11
12
13
14

AkashVerma-emgk
Автор

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

dhairyasheelpawar
Автор

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<Integer> list = missingNumbers(a);
System.out.println(list);
}

public static Set<Integer> missingNumbers(int[] num){
Set<Integer> missingnumber = new HashSet<Integer>();
Set<Integer> list = new HashSet<>(num.length);
for(int n : num){
list.add(n);
}
int max = num[0], min = num[0];
for(int i=1;i<num.length;i++){
if(num[i]<min)
min = num[i];
if(num[i]>max)
max= num[i];
}
for(int i=min;i<=max;i++){
if(!list.contains(i)){
missingnumber.add(i);
}
}
return missingnumber;
}
}

samahmahdi
welcome to shbcf.ru