Interview Question on Based Array

Sanjay Singh
3 min readDec 28, 2020

--

1-FindLargestSmallestNumberunsortedArray
input { 1, 200, 3, 4, 5, 7, 8, 9 }; o/p — 200 &1

public class FindLargestSmallestNumberunsortedArray {
public static String findNumber(int x[]) {
int min = x[0];
int max = x[0];
for (int i = 0; i < x.length; i++) {
if (max < x[i]) {
max = x[i];
}
if (min > x[i]) {
min = x[i];
}
}
return “MAX =” + max + “&” + “ “ + “MIN =” + min;
}
public static void main(String[] args) {
int x[] = { 1, 200, 3, 4, 5, 7, 8, 9 };
String t = FindLargestSmallestNumberunsortedArray.findNumber(x);
System.out.println(t);}}

Ques — FindMissingNumber number ?

package com.array;

public class FindMissingNumber {
public static int missingNumber(int[] x, int upto) {
int totelSum = 0, pratialSum = 0;
totelSum=(upto)*(upto+1)/2;
for (int a : x) {
pratialSum = pratialSum + a;
}
System.out.println(totelSum + “-” + pratialSum);
return totelSum — pratialSum;
}public static void main(String[] args) {
int x[] = { 1, 2, 3, 4, 5, 7, 8, 9 };
int missing = FindMissingNumber.missingNumber(x, 9);
System.out.println(missing);}}

Ques — Find DuplicateNumber …
package com.array;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class FinfDuplicateNumber {
public static Set<Integer> dublicate(int x[]) {
Set<Integer> list = new HashSet<>();

for (int i = 0; i < x.length; i++) {

for (int j = 0; j < x.length; j++) {

if (x[i] == x[j] && i != j) {
list.add(x[i]);
}}}
return list;}

Ques Counting frequencies of array elements*/
public static Map<Integer, Integer> dublicateElementWithCount(int x[]) {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();

for (int i = 0; i < x.length; i++) {
for (int j = 0; j < x.length; j++) {

if (map.containsKey(x[i])) {

map.put(x[i], map.get(x[i]) + 1);

}
map.put(x[i], 1);

}
}
return map;

}

public static void main(String[] args) {

int[] x = { 3, 4, 5, 6, 9, 9, 6, 6 };
Set<Integer> li = FinfDuplicateNumber.dublicate(x);
System.out.println(li);
Map<Integer, Integer> map = FinfDuplicateNumber.dublicateElementWithCount(x);
System.out.println(map);}}
package com.array;

Ques InterSectionOfTwoArray

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class InterSectionOfTwoArray {

/*
* Intersection between two integer arrays By Using Collection Api
*/
public static Set<Integer> intersection(Integer[] x, Integer[] y) {
Set<Integer> set = new HashSet<>();
set.addAll(Arrays.asList(x));
set.retainAll(Arrays.asList(y));
return set;
}

public static void main(String[] args) {
Integer[] a = { 0, 1, 2, 3, 4, 6, 7, 8, 9 };
Integer[] b = { 1, 3, 5, 7, 9 };

InterSectionOfTwoArray.intersection(a, b);

}}

Ques ReverseArrayUptoPossion

package com.array;

public class ReverseArrayUptoPossion {
public static void reverse(int possion, int[] x) {
int temp;

for (int i = 0; i < possion / 2; i++) {
temp = x[i];
x[i] = x[possion — i — 1];
x[possion — i — 1] = temp;

}
for (int y : x)
System.out.println(y);}

public static void main(String[] args) {

int[] x = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
ReverseArrayUptoPossion.reverse(4, x);}}

Find duplicates in a given array when elements are not limited to a range

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class Example {
public static void main(String[] args) {
int a[] = { 20, 30, 40, 30, 50, 50, 21, 1, 4, 30 };
Set<Integer> set = new HashSet<Integer>();
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i = 0; i < a.length; i++) {
for (int j = i + 1; j < a.length; j++) {
if ((a[i] == a[j]) && (i != j)) {
set.add(a[i]);
if (map.containsKey(a[i])) {
map.put(a[i], map.get(a[i]) + 1);
} else {
map.put(a[i], 1);}}}}
System.out.println(set);
System.out.println(map);
}}
o/p
[50, 30]
{50=1, 30=3}

--

--

Sanjay Singh
Sanjay Singh

Written by Sanjay Singh

Java, Spring Boot & Microservices developer Sharing knowledge, tutorials & coding tips on my Medium page. Follow me for insights & see story list section

No responses yet