Oracle Interview Question
Senior Software Development EngineersCountry: India
Interview Type: In-Person
class Main{
static void printFirstRepeating(int arr[]) {
int min = -1;
HashSet<Integer> set = new HashSet<>();
for (int i=arr.length-1; i>=0; i--) {
if (set.contains(arr[i]))
min = i;
else
set.add(arr[i]);
}
if (min != -1)
System.out.println("The first repeating element is " + arr[min]);
else
System.out.println("There are no repeating elements");
}
public static void main (String[] args) throws java.lang.Exception {
int arr[] = {10, 5, 3, 4, 3, 5, 6};
printFirstRepeating(arr);
}
}
import java.util.Map;
import java.util.HashMap;
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
int[] intArray = {5, 4, 6, 8, 9};
Map<Integer, Integer> freqCountMap = new HashMap<Integer, Integer>();
for(int a : intArray) {
Integer k = freqCountMap.get(a);
if(k != null) {
System.out.println("First duplicate occurrence is for " + a);
break;
}
else {
freqCountMap.put(a, 1);
}
}
}
}
import java.util.Map;
import java.util.HashMap;
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
int[] intArray = {5, 4, 6, 8, 9};
Map<Integer, Integer> freqCountMap = new HashMap<Integer, Integer>();
for(int a : intArray) {
Integer k = freqCountMap.get(a);
if(k != null) {
System.out.println("First duplicate occurrence is for " + a);
break;
}
else {
freqCountMap.put(a, 1);
}
}
}
}
import java.util.Map;
import java.util.HashMap;
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
int[] intArray = {5, 4, 6, 8, 9};
Map<Integer, Integer> freqCountMap = new HashMap<Integer, Integer>();
for(int a : intArray) {
Integer k = freqCountMap.get(a);
if(k != null) {
System.out.println("First duplicate occurrence is for " + a);
break;
}
else {
freqCountMap.put(a, 1);
}
}
}
}
class Main{
static void printFirstRepeating(int arr[]) {
int min = -1;
HashSet<Integer> set = new HashSet<>();
for (int i=arr.length-1; i>=0; i--) {
if (set.contains(arr[i]))
min = i;
else
set.add(arr[i]);
}
if (min != -1)
System.out.println("The first repeating element is " + arr[min]);
else
System.out.println("There are no repeating elements");
}
public static void main (String[] args) throws java.lang.Exception {
int arr[] = {10, 5, 3, 4, 3, 5, 6};
printFirstRepeating(arr);
}
}
class MyCode {
public static void main (String[] args) {
int[] numbers = {3,4,6,7,2,4,8,3,3,0};
HashSet<Integer> viewed = new HashSet<>();
int index = -1;
for(int i=0; i < numbers.length; i++) {
if(viewed.contains(numbers[i])) {
index = i;
break;
}else {
viewed.add(numbers[i]);
}
}
System.out.printf("index = %d, value=%d", index, numbers[index]);
}
}
#include<stdio.h>
int* first_dup(int *val,int value)
{
int b,a=5;
int *start=val;
while(a)
{
if (*val==value)
{
b=(val-start)+1;
val++;
while(b!=5)
{
if (*val==value)
return val;
else
b++;
val++;
}
}
else
val++;
a--;
}
printf("No matching value");
return NULL;
}
main()
{
int array[5],b,a=0;
while(a!=5)
{
printf("Enter the five values:");
scanf("%d",&b);
array[a]=b;
a++;
}
printf("Enter the value what to check the duplicate:");
scanf("%d",&b);
int *pos=first_dup(array,b);
b=(pos-array);
printf("the pos is : %d ",b);
}
#include<stdio.h>
int* first_dup(int *val,int value)
{
int b,a=5;
int *start=val;
while(a)
{
if (*val==value)
{
b=(val-start)+1;
val++;
while(b!=5)
{
if (*val==value)
return val;
else
b++;
val++;
}
}
else
val++;
a--;
}
printf("No matching value");
return NULL;
}
main()
{
int array[5],b,a=0;
while(a!=5)
{
printf("Enter the five values:");
scanf("%d",&b);
array[a]=b;
a++;
}
printf("Enter the value what to check the duplicate:");
scanf("%d",&b);
int *pos=first_dup(array,b);
b=(pos-array);
printf("the pos is : %d ",b);
}
#include<stdio.h>
int* first_dup(int *val,int value)
{
int b,a=5;
int *start=val;
while(a)
{
if (*val==value)
{
b=(val-start)+1;
val++;
while(b!=5)
{
if (*val==value)
return val;
else
b++;
val++;
}
}
else
val++;
a--;
}
printf("No matching value");
return NULL;
}
main()
{
int array[5],b,a=0;
while(a!=5)
{
printf("Enter the five values:");
scanf("%d",&b);
array[a]=b;
a++;
}
printf("Enter the value what to check the duplicate:");
scanf("%d",&b);
int *pos=first_dup(array,b);
b=(pos-array);
printf("the pos is : %d ",b);
}
import java.util.Set;
import java.util.HashSet;
class FindFirstDuplicateOccurence{
public static int findFirstDuplicate(int[] arr){
Set<Integer> visited = new HashSet<>();
for(int i=0;i<arr.length;++i){
if(visited.contains(arr[i]))
return i;
else
visited.add(arr[i]);
}
return -1;
}
public static void main(String[] args) {
// int[] arr = {7, 5, 3, 5, 7, 3, 7, 5};
int[] arr = {1, 2, 3};
int i = findFirstDuplicate(arr);
if(-1==i)
System.out.println("no duplicates");
else
System.out.println("first duplicate value = "+arr[i]);
}
}
- Anupom Chakrabarty July 05, 2018