18) Find sum of elements in an array using recursion?
class SumOfArray { public static void main (String[] args) { int[] arr = {1,2,3,4,5}; int len = arr.length; System.out.println("The addition is: " + addition(arr, len)); } public static int addition(int[] arr, int len){ if(len == 0){ return 0; } return arr[len-1] + addition(arr, len-1); } }
19) Write a java program to find duplicate elements in an array?
Output:
Found duplicate 3
Found duplicate 4
Found duplicate 6
20) Find largest number in an array
Output: 17
21) To check equality of 2 arrays
Output: Both arrays are not Equal
22) Find third largest number in array
23) Reverse Array
Output: 654321
24) Find second highest number in an array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public class FindDuplicatesInArray { public static void main(String[] args) { int[] arr = {3,4,6,6,3,4}; for (int i = 0; i < arr.length; i++) { for (int j = i + 1; j < arr.length; j++) { if (arr[i] == (arr[j])) { System.out.println("Found duplicate " + arr[i]); } } } } } |
Found duplicate 3
Found duplicate 4
Found duplicate 6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import java.util.ArrayList; public class FindMax { public static int max; public static void main(String[] args) { int[] arr = {3,14,16,17,3,4}; for (int i = 0; i < arr.length-1; i++) { if(arr[i]>arr[i+1]) { max = arr[i]; } } System.out.println(max); } } |
21) To check equality of 2 arrays
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | public class ArrayEquals { public static int max; public static boolean isEquals = true; public static void main(String[] args) { int[] arr1 = { 3, 14, 16, 17, 3, 4 }; int[] arr2 = { 3, 14, 16, 17, 3, 4, 3}; if (arr1.length == arr2.length) { for (int i = 0; i < arr1.length - 1; i++) { if (arr1[i] != arr2[i]) { System.out.println("Both arrays are not Equal"); isEquals = false; break; } } if (isEquals) System.out.println("Both arrays are Equal"); } else { System.out.println("Both arrays are not Equal"); } } } |
22) Find third largest number in array
public class Xlargest { public static void main(String[] args) { int which_largest = 3; int[] arr = { 3, 14, 6, 17, 5, 4 }; //Binary search logic for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr.length - 1; j++) { if (arr[j] > arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } //Printing X largest value System.out.println(arr[arr.length - which_largest]); } }
23) Reverse Array
public class ReverseArray { public static void main(String[] args) { ReverseArray obj = new ReverseArray(); int[] arr = { 1, 2, 3, 4, 5, 6 }; System.out.println("Before : "); obj.printArray(arr); obj.reverseArray(arr, 0, arr.length - 1); System.out.println("After : "); obj.printArray(arr); } public void reverseArray(int[] arr, int start, int end) { while (start < end) { int temp = arr[start]; arr[start] = arr[end]; arr[end] = temp; start = start + 1; end = end - 1; } } public void printArray(int[] arr) { for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } } }
24) Find second highest number in an array.
class SecondHighest{ public static void main (String[] args) { int[] arr = {5,8,2,9,10}; System.out.print(secondMax(arr)); } public static int secondMax(int[] arr) { int max = arr[0]; int smax = arr[0]; for(int i = 0; i < arr.length; i++){ if(arr[i] > max){ max = arr[i]; } if(arr[i] < max && arr[i] > smax) smax = arr[i]; } return sMax; } }
Output: 9
25) Find common elements in two different arrays
public class CommonElementArrays { public static void main(String[] args) { int[] arr = {1,2,3,4,5,6}; int[] arr1 = {8,6,4,9,2}; for(int i=0;i<arr.length;i++) { for (int j = 0; j < arr1.length; j++) { if (arr[i] == arr1[j]) { System.out.println(arr[i]); } } } } }
No comments:
Post a Comment