Write a program to find missing element in Array
How to find Missing Element
- Create an Array int[]arr=new int[6] //array size 6 but you can change
- Find expected length of an array
- Find value (sum is variable) of sum by this : expected*(expected+1)/2;
- create variable add put value in add = 0;
- using for loop traverse the array and assign value into add variable : add=add+array[i];
- finally print value of an array sum-add;
Program :
public class MissingElement
{
public static void main(String[] args) {
int array[]={1,2,3,5,6,7,8,9,10};
int expected=array.length+1;
int sum=expected*(expected+1)/2;
int add=0;
for (int i=0;i<array.length;i++){
add=add+array[i];
System.out.println("Missing Element is "+(sum-add));
}
}
}
Output :
Click to next or learn with your choice
- 4 ways to print Array in Java
- 2-D Array in Java
- Anonymous Array in Java
- Single Dimensional Array in Java
- Find Second Largest Number in Array
- Delete an Element in Array in Java
- Common Element in Array in Java
- Missing Element in Array
- Insert Element in Array
- Reverse Array in Java
- Merge Two Array in Java
- Smallest and Largest Elements in Array
- Find Odd and Even Number in Array
Sorting and Searching
Programs
Start with your
Choice
-Important Programs for Freshers
- Palindrome Number
- Prime
Number
- Swapping
Number
- Leap
Year
- Odd
and Even Number
- Fibonacci
Series
- Armstrong Number
- Factorial
Number
- Print
Table
Learn Pattern Programs
- Alphabet
pattern program in java
- Star Pattern Programs in Java
- Square
pattern programs in java
- Diamond
Star pattern programs in java
- Number
Pattern Programs in java
- Diagonal pattern program in java
Click
to Learn MySQL
No comments:
Post a Comment