How to find missing elements of a given range in array of distinct elements with C#

1 Answer

0 votes
using System;
using System.Collections.Generic;

public class Program
{
	public static void printMissingElements(int[] arr, int range_start, int range_end)
	{
		HashSet<int> hset = new HashSet<int>();
		for (int i = 0; i < arr.Length; i++) {
			hset.Add(arr[i]);
		}

		for (int i = range_start; i <= range_end; i++) {
			if (!hset.Contains(i)) {
				Console.Write(i + " ");
			}
		}
	}
	public static void Main(string[] args)
	{
		int[] arr = new int[] {2, 4, 5, 7, 9};
		int range_start = 1, range_end = 9;

		printMissingElements(arr, range_start, range_end);
	}
}




/*
run:
 
1 3 6 8 
 
*/

 



answered May 23, 2023 by avibootz
...