Java Interview Questions

Showing 1 - 10 of 29 results

Java program to find one missing number in an array of 1 to N

You are given an integer array containing 1 to n but one of the number from 1 to n in the array is missing. You need to provide optimum solution to find the missing number. Number can not be repeated in the array. 

For Example 

int[] arr1 = {1, 5, 4, 2}; 3 is missing
int[] arr1 = {7, 5, 4, 1, 2, 3}; 6 is missing

Java program to find common elements between two arrays.

Below program will compare elements of 2 array and display duplicates elements.

package com.seleniumtest;

public class CommonArrayElements 
{
	public static void main(String args[])
	{
		int[] firstArray = {1, 4, 2, 3, 7, 5};
		int[] secondArray = {3, 4, 1, 6};
	
		for(int i=0; i < firstArray.length; i++)
		{
			for(int j=0; j<secondArray.length; j++)
			{
				if(firstArray[i] == secondArray[j])
				{
					System.out.println(firstArray[i] + " ");
				}
			}
		}
	}
}

 

Subscribe to Java Interview Questions