Java Array Manipulation Assignments
Java Array Manipulation Assignments
Input/Output:
Input array: 1, 2, 3, 4, 6, 7
Output:
Missing element is: 5
Solution:-
class A31{
public static void main(String args[]){
int[] arr={1,2,3,4,6,7};
int actualsum=0;
for(int i=0;i<[Link];i++){
actualsum += arr[i];
}
int expectedsum= 1+2+3+4+5+6+7;
int missingnum = expectedsum-actualsum;
[Link]("Missing Number : "+missingnum);
}
}
Output:-
Q2. Given an array of integers and we
have to find their average .
Input/Output:
Enter number of elements you want in
array : 10
Enter all the elements :
65
45
25
65
84
74
74
15
36
Sum of the array elements is : 579
Average of the array elements is : 57.9
Solution:-
import [Link].*;
class A32{
public static void main(String args[]){
Scanner sc=new Scanner([Link]);
[Link]("Enter number of element you want in array : ");
int num=[Link]();
[Link]("Enter all element :");
int sum=0;
float average;
for(int i=0;i<[Link];i++){
arr[i]=[Link]();
sum +=arr[i];
}
average=(float)sum/num;
}
}
Output:-
Q3. Move all zero at the end of the array
Given an integer array with zeros (0’s) and we have to move all zeros at
the end of the array
Input/Output:
Input array: 5, 1, 6, 0, 0, 3, 9, 0, 6, 7, 8, 12, 10, 0, 2
After moving 0 at the end
Output array: 5, 1, 6, 3, 9, 6, 7, 8, 12, 10, 2, 0, 0, 0, 0
Solution:-
class A33{
public static void main(String args[]){
int[] arr={5,1,6,0,0,3,9,0,6,7,8,12,10,0,2};
int index=0;
for(int i=0;i<[Link];i++){
if(arr[i] != 0){
arr[index++]=arr[i];
}
}
while(index<[Link]){
arr[index++]=0;
}
[Link]("After moving 0 at the end : ");
for(int num : arr){
[Link](num+" ");
}
}
}
Output:-
}
if(index==-1){
[Link]("Element not found in the array");
} else{
for(int i=index;i<[Link]-1;i++){
arr[i]=arr[i+1];
}
}
}
Output:-
Q5. Print EVEN and ODD elements from an array
Given a one dimensional array and we have to print its EVEN and ODD
elements separately.
Input/Output:
Input:
Given array (elements will be read in program): 10 11 12 13 14
Output:
Odd numbers in the array are : 10 12 14
Even numbers in the array are : 11 13
Solution:-
import [Link].*;
class A35{
public static void main(String args[]){
for(int i =0;i<[Link];i++){
if(arr[i]%2==0){
evenarr[evencount++]=arr[i];
} else{
oddarr[oddcount++]=arr[i];
}
}
for(int i=0;i<evencount;i++){
[Link](evenarr[i]+" ");
}
[Link]();
for(int i=0;i<oddcount;i++){
[Link](oddarr[i]+" ");
}
[Link]();
}
}
Output:-
Q6. Merge two one dimensional arrays
Given two one-dimensional arrays and we have to merge them using
java program.
Input/Output:
Input:
Array 1 (elements will be read in program): 1 2 3 4 5 6 7 8 9 10
Array 2 (elements will be read in program): 11 12 13 14 15
Output:
New array (After merging elements)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Solution:-
class A36{
public static void main(String args[]){
int[] arr1={1,2,3,4,5,6,7,8,9,10};
int[] arr2={11,12,13,14,15};
}
}
Output:-
Q9. Remove duplicate elements from an array
Given an array of integers and we have to remove duplicate elements
using java program.
Input/Output:
Input array elements:
1, 2, 3, 1, 2, 3, 4
Output:
Elements after removing duplicates
1, 2, 3, 4
Solution:-
import [Link].*;
class A39{
public static void main(String args[]){
Scanner obj= new Scanner([Link]);
[Link]("Enter value of array : ");
int arr[]=new int[7];
int n=[Link]-1;
int m=0;
for(int i=0;i<[Link];i++){
arr[i]=[Link]();
}
for(int i=0;i<[Link];i++){
for(int j=i+1;j<n;j++){
if(arr[j]==arr[i]){
m=arr[n];
arr[n]=arr[j];
arr[j]=m;
n--;
}
}
}
for(int i=0;i<n;i++){
[Link](arr[i]+" ");
}
}
}
Output:-
Q10. Find second largest element in an array
Given an array of N integers and we have to find its second largest
element using Java program.
Input/Output:
Input:
Enter number of elements: 4
Output:
Second largest element in: 45
Solution:-
import [Link].*;
class A310{
public static void main(String args[]){
Scanner obj=new Scanner([Link]);
[Link]("Enter number of element : ");
int num=[Link]();
int arr[]=new int[num];
int max=Integer.MIN_VALUE;
int smax=Integer.MIN_VALUE;
for(int i=0;i<[Link];i++){
[Link]("Input element : ");
arr[i]=[Link]();
}
for(int i=0;i<[Link];i++){
if(arr[i]>max){
smax=max;
max=arr[i];
} else if(arr[i]>smax && arr[i]!=max){
smax=arr[i];
}
}
[Link]("Second Largest element is : "+smax);
}
}
Output:-
for(int i=0;i<[Link];i++){
[Link]("Input element : ");
arr[i]=[Link]();
}
for(int i=0;i<[Link];i++){
if(arr[i]<min){
smin=min;
min=arr[i];
} else if(arr[i]<smin && arr[i]!=min){
smin=arr[i];
}
}
[Link]("Second Smallest element is : "+smin);
}
}
Output:-
Q12. Find smallest element in an array
Given an array of N integers and we have to find its minimum/smallest
element using Java program.
Input/Output:
Input:
Enter number of elements: 4
Input elements: 45, 25, 69, 40
Output:
Smallest element in: 25
Solution:-
import [Link].*;
class A312{
public static void main(String args[]){
Scanner obj=new Scanner([Link]);
[Link]("Enter number of element : ");
int num=[Link]();
int arr[]=new int[num];
int min=Integer.MAX_VALUE;
for(int i=0;i<[Link];i++){
[Link]("Input element : ");
arr[i]=[Link]();
}
for(int i=0;i<[Link];i++){
if(arr[i]<min){
min=arr[i];
}
}
[Link]("Smallest element is : "+min);
}
}
Output:-
int pcount=0;
int ncount=0;
int zcount=0;
for(int i=0;i<[Link];i++){
if(arr[i]>0){
pcount++;
} else if(arr[i]<0){
ncount++;
} else if(arr[i]==0){
zcount++;
}
}
[Link]("Posotive number are : "+pcount);
[Link]("Negative number are : "+ncount);
[Link]("Zero are : "+zcount);
}
}
Output:-
Q14. Access elements of character array using for each loop
In this program, we will create an array of characters and access its
elements using for each loop and print it on the screen.
Input/Output:
Input:
char charArray[] = {'A', 'B', 'C', 'D',
'E', 'F'};
Output:
Array elements:
A
B
C
D
E
F
Solution:-
class A314{
public static void main(String args[]){
char[] arr={'A','B','C','D','E'};
int i;
for(char x:arr){
[Link](x);
}
}
}
Output:-
for(int i=0;i<[Link];i++){
num[i]=[Link]();
}
[Link]("Enter item to serach : ");
int item=[Link]();
Boolean isFound=false;
for(int i=0;i<[Link];i++){
if(num[i]==item){
[Link]("Element found at position "+i);
isFound=true;
break;
}
}
}
}
Output:-
for(int i=0;i<[Link];i++){
if(arr[i]==item){
[Link]("Item found at index "+i);
isFound=true;
break;
}
}
}
}
Output:-
Q18. This is the Java Program to Find Repeated Elements and the
Frequency of Repetition.
Problem Description
Given an array of integers, find and print the repeated elements and
their
frequency of repetition.
Example:
Array: [1,2,3,4,5,5,3]
Solution:-
class A318{
public static void main(String args[]){
int n=1234553;
int freq[]=new int[6];
while(n!=0){
int r=n%10;
freq[r]++;
n=n/10;
}
[Link]("Frequecy with number ");
for(int i=0;i<[Link];i++){
if(freq[i]!=0){
[Link]("frequency of "+i+" is = "+freq[i]);
}
}
}
}
Output:-
Q22. Given an array of integers, print the even numbers present at even
index numbers.
Example:
Array = [2,1,4,4,5,7]
Output = 2 4
Solution:-
class A322{
public static void main(String args[]){
int[] arr={2,1,4,4,5,7};
[Link]("Output is : ");
for(int i=0;i<[Link];i++){
if(i%2==0 && arr[i]%2==0){
[Link](arr[i]+" ");
}
}
}
}
Output:-
for(int i=0;i<n-1;i++){
for(int j=i+1;j<n;j++){
if(names[i].compareTo(names[j])>0){
String temporay=names[i];
names[i]=names[j];
names[j]=temporay;
}
}
}
[Link]("Names in sorted order : ");
for(int i=0;i<n;i++){
[Link](names[i]);
}
}
}
Output:-
for(int i=0;i<[Link];i++){
if(arr[i]!=0){
arr[index++]=arr[i];
}
}
while(index<[Link]){
arr[index++]=0;
}
[Link]("Array element after shif to end : ");
for(int num : arr){
[Link](num+" ");
}
}
}
Output:-
Q29. Given an array arr of n elements that is first strictly increasing and
then maybe strictly decreasing, find the maximum element in the array.
Note: If the array is increasing then just print the last element will be
the
maximum value.
Example:
Input: array[]= {5, 10, 20, 15}
Output: 20
Explanation: The element 20 has neighbors 10 and 15, both of them are
less than 20.
Input: array[] = {10, 20, 15, 2, 23, 90, 67}
Output: 20 or 90
Solution:-
class A329 {
public static void main(String args[]){
int[] arr={5,10,20,15};
int[] arr1={10,20,15,2,23,90,67};
for(int i=1;i<[Link];i++){
if(arr[i]>arr[i-1] && arr[i]>arr[i+1]){
[Link]("Output : "+arr[i]);
}
}
[Link]("Output : ");
for(int i=1;i<[Link];i++){
if(arr1[i]>arr1[i-1] && arr1[i]>arr1[i+1]){
[Link](arr1[i]+" ");
}
}
}
}
Output:-
}
}
Output:-
for(int i=2;i<=[Link]-2;i++){
sum +=arr[i];
}
[Link]("Sum found between indexes 2 and 4 : "+sum);
}
}
Output:-
}
}
Output:-
for(int i=0;i<[Link];i++){
if(arr[i]>largest){
largest=arr[i];
}
else if(arr[i]<smallest){
smallest=arr[i];
}
}
[Link]("Largest number in array : "+largest);
[Link]("Smallest number in array : "+smallest);
}
}
Output:-
Q36. Reverse int and String array
Output: Original int array : [101, 102, 103, 104, 105]
reversed int array : [105, 104, 103, 102, 101]
Solution:-
class A336{
public static void main(String args[]){
int [] arr={101,102,103,104,105};
}
}
Output:-
int x=2013;
for(int i=0;i<[Link];i++){
if(arr[i]==x){
[Link]("True");
}
}
}
}
Output:-
int x=25;
[Link]("Index position of 25 is : ");
for(int i=0;i<[Link];i++){
if(arr[i]==x){
[Link](i);
}
}
}
}
Output:-
Q39. Write a program to remove a specific element from an array.
Solution:-
class A339{
public static void main(String args[]){
int arr[]={25,14,56,15,36};
int ed=25;
int index =-1;
for(int i=0;i<[Link];i++){
if(arr[i]==ed){
index=i;
break;
}
}
if(index==-1){
[Link]("Element not found in the array");
} else{
for(int i=index;i<[Link]-1;i++){
arr[i]=arr[i+1];
}
}
}
Output:-
Q40. Matrix Multiplication Program
In case of matrix multiplication, one row element of first matrix is
multiplied by all columns of second matrix.
Solution:-
import [Link].*;
class A340{
public static void main(String args[]){
Scanner obj=new Scanner([Link]);
int[][] arr=new int[3][3];
int[][] arr1=new int[3][3];
int[][] arr2=new int[3][3];
[Link]("Enter element of Matrix 1");
for(int r=0;r<[Link];r++){
for(int c=0;c<[Link];c++){
[Link]("Enter element if Index "+r+","+c+": ");
arr[r][c]=[Link]();
}
}
[Link]("Enter element of Matrix 2");
for(int r=0;r<[Link];r++){
for(int c=0;c<[Link];c++){
[Link]("Enter element if Index "+r+","+c+": ");
arr1[r][c]=[Link]();
}
}
for(int r=0;r<[Link];r++){
for(int c=0;c<[Link];c++){
arr2[r][c]=arr[r][c]*arr1[r][c];
[Link](arr2[r][c]+" ");
}
[Link]();
}
}
}
Output:-
}
}
Output:-
Q42. Find second largest element in an array
Given an array of N integers and we have to find its second largest
element
Input/Output:
Input:
Enter number of elements: 4
Input elements: 45, 25, 69, 40
Output:
Second largest element in: 45
Solution:-
import [Link].*;
class A342{
public static void main(String args[]){
Scanner obj=new Scanner([Link]);
[Link]("Enter number of elements : ");
int n=[Link]();
int[] arr=new int[n];
for(int i=0;i<[Link];i++){
if(arr[i]>0){
pcount++;
}
else if(arr[i]<0){
ncount++;
}
else if(arr[i]==0){
zcount++;
}
}
[Link]("Positive Number are : "+pcount);
[Link]("Negative Number are : "+ncount);
[Link]("Zero Number are : "+zcount);
}
}
Output:-
Q45. Find the length of an array
In this program, we will create an array of 10 integers then we will find
the length of the array
Input/Output:
Input:
int[] intArr = new int[10];
Output:
Length of array is: 10
Solution:-
class A345{
public static void main(String args[]){
int[] arr=new int[10];
[Link]("length of array : "+[Link]);
}
}
Output:-
for(int i=0;i<n;i++){
if(arr[i]>flargest){
slargest=flargest;
flargest=arr[i];
} else if(arr[i]>slargest && arr[i]!=flargest){
slargest=arr[i];
}
}
[Link]("The first largest is : "+flargest);
[Link]("The second largest is : "+slargest);
}
}
Output:-
}
[Link]();
[Link]("Odd values of array : ");
for(int i=0;i<n;i++){
if(arr[i]%2!=0){
[Link](arr[i]+" ");
}
}
[Link]();
}
}
Output:-