Array interview programs
1.write a java program to find duplicate element in an array.
package com.howtodoinjava.interview;
import java.util.HashSet;
import java.util.Set;
public class DuplicateInArray
{
public static void main(String[] args)
{
int[] array = {1,1,2,3,4,5,6,7,8,8};
Set<Integer> set = new HashSet<Integer>();
for(int i = 0; i < array.length ; i++)
{
//If same integer is already present then add method will
return FALSE
if(set.add(array[i]) == false)
{
System.out.println("Duplicate element found : " +
array[i]);
}
}
}
}
Output:
Duplicate element found : 1
Duplicate element found : 8
2.> write a java program to find second largest number in an array.
1. public class SecondLargestInArrayExample{
2. public static int getSecondLargest(int[] a, int total){
3. int temp;
4. for (int i = 0; i < total; i++)
5. {
6. for (int j = i + 1; j < total; j++)
7. {
8. if (a[i] > a[j])
9. {
10. temp = a[i];
11. a[i] = a[j];
12. a[j] = temp;
13. }
14. }
15. }
16. return a[total-2];
17. }
18. public static void main(String args[]){
19. int a[]={1,2,5,6,3,2};
20. int b[]={44,66,99,77,33,22,55};
21. System.out.println("Second Largest: "+getSecondLargest(a,6));
22. System.out.println("Second Largest: "+getSecondLargest(b,7));
23. }}
Output:
Second Largest: 5
Second Largest: 77
3.> how to find missing number in the array.
import java.util.*;
public class Exercise24 {
public static void main(String[] args) {
int total_num;
int[] numbers = new int[]{1,2,3,4,6,7};
total_num = 7;
int expected_num_sum = total_num * ((total_num + 1) / 2);
int num_sum = 0;
for (int i: numbers) {
num_sum += i;
}
System.out.print( expected_num_sum - num_sum);
System.out.print("\n");
}
}
Sample Data: 1,2,3,4,6,7
4.> how to find number occuring odd number of times in an array.
public class
OddOccuringNumbe
r {
public static int findNumber(int [] A){
int x=0;
for(int i=0;i<A.length;i++){
x= x^A[i];
}
return x;
}
public static void main(String[] args) {
int[] A = { 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 5, 6, 6, 6, 6, 7, 7 };
System.out.println("Element appearing add number of times: " + findNumber(A));
}
}
Element appearing add number of times: 5
5.> Write a java program to count occurrences of each element in an array.
1. import java.util.Scanner;
2. public class Count_Occurrence
3. {
4. public static void main(String[] args)
5. {
6. int n, x, count = 0, i = 0;
7. Scanner s = new Scanner(System.in);
8. System.out.print("Enter no. of elements you want in array:");
9. n = s.nextInt();
10. int a[] = new int[n];
11. System.out.println("Enter all the elements:");
12. for(i = 0; i < n; i++)
13. {
14. a[i] = s.nextInt();
15. }
16. System.out.print("Enter the element of which you want to count
number of occurrences:");
17. x = s.nextInt();
18. for(i = 0; i < n; i++)
19. {
20. if(a[i] == x)
21. {
22. count++;
23. }
24. }
25. System.out.println("Number of Occurrence of the Element:"+count);
26. }
27. }
Output:
$ javac Count_Occurrence.java
$ java Count_Occurrence
Enter no. of elements you want in array:5
Enter all the elements:
2
3
3
4
3
Enter the element of which you want to count number of occurrences:3
Number of Occurrence of the Element:3
7.> How to find the missing element in integer array of 1 to 7.
import java.io.*;
class GFG
{
static int search(int ar[],
int size)
{
int a = 0, b = size - 1;
int mid = 0;
while ((b - a) > 1)
{
mid = (a + b) / 2;
if ((ar[a] - a) != (ar[mid] - mid))
b = mid;
else if ((ar[b] - b) != (ar[mid] - mid))
a = mid;
}
return (ar[mid] + 1);
}
// Driver Code
public static void main (String[] args)
{
int ar[] = { 1, 2, 3, 4, 5, 6, 8 };
int size = ar.length;
System.out.println("Missing number: " +
search(ar, size));
}
}
// This code is contributed
// by inder_verma.
Output:
Missing number: 7
8.> How to cut or remove an element from the array.
import java.util.Arrays;
public class Exercise7 {
public static void main(String[] args) {
int[] my_array = {25, 14, 56, 15, 36, 56, 77, 18, 29, 49};
System.out.println("Original Array : "+Arrays.toString(my_array));
// Remove the second element (index->1, value->14) of the array
int removeIndex = 1;
for(int i = removeIndex; i < my_array.length -1; i++){
my_array[i] = my_array[i + 1];
}
// We cannot alter the size of an array , after the removal, the last
and second last element in the array will exist twice
System.out.println("After removing the second element:
"+Arrays.toString(my_array));
}
}
O/P
Original Array : [25, 14, 56, 15, 36, 56, 77, 18, 29, 49]
After removing the second element: [25, 56, 15, 36, 56, 77, 18,
29, 49, 49]
9.> How to get largest and smallest number in an array.
/*
Find Largest and Smallest Number in an Array Example
This Java Example shows how to find largest and smallest number in an
array.
*/
public class FindLargestSmallestNumber {
public static void main(String[] args) {
//array of 10 numbers
int numbers[] = new int[]{32,43,53,54,32,65,63,98,43,23};
//assign first element of an array to largest and smallest
int smallest = numbers[0];
int largetst = numbers[0];
for(int i=1; i< numbers.length; i++)
{
if(numbers[i] > largetst)
largetst = numbers[i];
else if (numbers[i] < smallest)
smallest = numbers[i];
System.out.println("Largest Number is : " + largetst);
System.out.println("Smallest Number is : " + smallest);
}
}
/*
Output of this program would be
Largest Number is : 98
Smallest Number is : 23
*/