How to Find Duplicate Elements in an Array - Java Program | Java Interview Question and Answer #java

preview_player
Показать описание
How to Find Duplicate Elements in an Array - Java Program | Java Interview Question and Answer | Test Automation Central

Automation Testing Interview Question | Selenium WebDriver Java Interview Question | Automation Tutorials | Selenium Tutorials | Testing Automation Tutorials | Test Automation | Java Selenium | Automation Interview Question And Answers | ChromeDriver Class | WebDriver Interface

#sdet #seleniumtutorial #automationtester #automationtesting #javaprogramming #javatutorial #javaprogram #javaquestions #testautomation #testautomationcentral #seleniumtutorialforbeginners #seleniumwebdriver #javaselenium #seleniuminterview #finaljava
#seleniumforbeginners #sdetinterview #seleniumquestions #chromedriver #webdriver #webdriverinterface #seleniumwebdriver #windowhandles #javaprogram #javaprogramming #javaarrays
Рекомендации по теме
Комментарии
Автор

U can sort array, iterate array and check array index i and i +1 until end of array… constant space and O(nlogn) because of sort…. Or use hash map for linear time but also linear space

ViktorTheRook
Автор

It doesn't work when you have multiple duplicate elements in same array

arsalanalam
Автор

I'm just now learning about data structures and algorithms in C++. Correct me if I'm wrong, but I think this approach has a time complexity of O(n^2) because of the nested for loops, which is not that good if the arrays are really large. Is there a better/more efficient way to find duplicates in an array in Java?

cicartaya
Автор

Using hashmap and print the elements whose frequency is greater than 1 easy pezzy

anubhav.codess
Автор

Time complexity is O(n**2). You can easily use HasMap and one for loop. I can provide JS code. And time complexity for this code will be O(n)
function findDuplicates(arr) {
const elementCount = {};
const duplicates = [];

arr.forEach((element) => {
// Initialize count for the element or increment existing count
elementCount[element] = (elementCount[element] || 0) + 1;

// Check if the element is a duplicate and hasn't been added to duplicates array yet
if (elementCount[element] === 2) {
duplicates.push(element);
}
});

return duplicates;
}

akaki_khotcholava
Автор

No need for two for loops..it can be done in one

vedantjoshi
Автор

Instead of brute force, try to implement other approach like hsshmap.

crosswalker
Автор

Don't use 2 for loop, don't use set/hashset

mySofCh
Автор

why not just use "for each " element?..

josejoy
Автор

it doesnt work for 10000 elements arra

rubenbarkhudaryan
Автор

or you can use list
arr.stream().filter(x -> x == 1).toList();

eccomercebeast
Автор

What if all numbers were same let's say 1

dekhlohhgftgdrf