Java Program to find the first duplicate occurence in an array

preview_player
Показать описание
#learnwithkrishnasandeep #javacodinginterviewquestions #javaexamples #javaprograms #javatutorials #javaprogramming write a Java Program to find the first duplicate occurence in an array

In this video we are going to find out the first duplicate occurrence of a number in an array from given numbers.

Watch most important Java Coding Interview Questions and answers :

1) How to read a matrix from console in java?

2) How to remove duplicates from string in java?

3) Write a java program to duplicate characters with number of occurences in java?

4) Write a java program to implement bubble sort part 1?

5) Write a java program to check string is palindrome or not?

6) Write a java program to print floyds triangle?

7) Write a java program to print floyds triangle?





8) How to count number of vowels in a string in java?

9) How to find largest and smallest values in an array in java?

10) Write java program to find duplicate elements in array in java?

11) write a java program to check the input number is prime or not in java?

12) Write a java program to print pyramid pattern of numbers?

13) Write a java program to find missing number in array in java?

14)Write a java program to print numbers in java up to 10 with out using loops?

15) How to sort elements in array in asc and desc order in java?



16) Write a java program to find factorial of a number in java?

17) write a java program to find second largest element in the array in the java?

18) Write a java program to check two Strings are anagrams or not by sorting and comparing strings?

19) Write java program to print Characters count in each word in String ?

20) Write a java program to find reverse of a string in java?

21) Write a java program to add elements in an array in java?

22) Write a java program to find largest of three numbers?



23) Write a java program to print multiplication table?

24) Write a java program to print star pattern?

25) Write a java program to find factors of number from 1 to 100

26) Write a java program to print fibonacci series using loops?

27) Write a java program to print Ascii values of a Character ?

28) write a java program to implement bubble sort - Part 2?

29) Write a java program to print fibonacci series ?

30) Write a java program to sort characters in String in java?

31) How to sort hashmap keys in java?

32) Write a java program to check two strings are anagrams or not?

33) Write a java program to find factorial for large number?

34) Write a java program to find even and odd number?

35) How to Swap two numbers without using third variable in java?(Addition and subtraction operation)

36) Write a java program to convert Fahrenheit to celsius?

37) Write a java program to find reverse of a number in java?

38) Write a java program to print alphabets?

39) Write a java program to check given input character is alphabet or not ?

Рекомендации по теме
Комментарии
Автор

Since we are using nested loops, its a brute force technique where the complexity becomes O(n2), Instead we can use just one loop and store it in a HashMap and check whether the element encountered was second occurance or not.

sgh
Автор

It's time complexity is O(n^2). If this answer given in oracle... They will not consider at all

PathimelaArun
Автор

Instead of return you can use break directly we get first duplicate

mohammedajazquadri
Автор

public class DuplicateInArray {
public static void main(String[] args) {
int[] a = new int[] {1, 4, 2, 3, 1, 55, 6};
boolean flag;
Map<Integer, Integer> map = new HashMap<>();
for(int i=0;i<a.length;i++) {
if(map.containsKey(a[i])) {
System.out.println("The first duplicate in array is - " + a[i]);
flag=true;
}else {
map.put(a[i], 1);
}
}
if(!flag)
System.out.println("No duplicate found");
}
}

varunshrivastava
Автор

If array is in order like..{1, 2, 3, 4, 3, 2, 4}...it prints 2...but by seeing an array we can say that 3 is 1st duplicated and then 2 ...but ur program op's 2 ..

Nodankumar
Автор

sir if add two duplicate value in array than it give all duplicate value not only first?

amanjainwal
Автор

if i give more duplicate numbers then it gives all those number but i need only 1st dubulicate
{1, 2, 3, 1, 4, 5, 4} it gives 1, 4 both but i need only 1

chaitanyachundiwar
Автор

you wrote O(N^2) this is bad you can do it with O(N) and cleaner
int[] arr=new int[]{1, 1, 1, 4, 4, 6, 7, 8, 9};


for(int i=0; i<str.length; i++){
int j=Math.abs(arr[i]);
if(arr[j]>=0)
arr[j]=-arr[j];
else
System.out.println(j+" ");

}

HvujES
Автор

when i run the code why only first number is always output even i have given duplicate number in array???

abhimanyuchouhan
Автор

public class FirstRepeatingElement1 {
public static void main(String[] args) {
int a[]= {1, 2, 3, 4, 5, 6, 1, 3};
boolean flag=false;
Map<Integer, Integer> map=new HashMap<Integer, Integer>();
for(int i=0;i<a.length;i++) {
if(map.containsKey(a[i])) {

break;

}else {
map.put(a[i], 1);
}
}
}
}

venkateswararaopattapu
welcome to shbcf.ru