How to Find Duplicate Elements in an Array - Java Program | Java Interview Question and Answer
ฝัง
- เผยแพร่เมื่อ 19 ก.พ. 2023
- How to Find Duplicate Elements in an Array - Java Program | Java Interview Question and Answer | Test Automation Central
For source code, visit - testautomationcentral.com/how...
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 - แนวปฏิบัติและการใช้ชีวิต
For detailed tutorials and source code, visit - testautomationcentral.com/how-to-find-duplicate-elements-in-an-array-java-program-java-interview-question-and-answer/
No need for two for loops..it can be done in one
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;
}
lol your code is worse . check your space complexity xD. I can solve all this in O(n) lol.
@@JohnWickea You must be a genius.
taking extra space is also not a good solution
there is a much better way to handle this case with no extra space and O(n) time complexity
@@ladro_magro5737 let the kimd live in it's own universe, since he's not listening to anyone 😂
any operation on hashmap cost O(log N) time
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
Just use a hashmap,
Hey bro can we use two pointer for this question?👀
Don't use 2 for loop, don't use set/hashset
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?
Sets...
It takes combination(array.size(), 2) iteration
Using hashmap and print the elements whose frequency is greater than 1 easy pezzy
Bro it is for the people who are only say core Java experience not collection
We can simply use stream for this
In interview they will tell u stream is doing fine. Create your logic then you have to learn basic bro
Instead of brute force, try to implement other approach like hsshmap.
why not just use "for each " element?..
it doesnt work for 10000 elements arra
It doesn't work when you have multiple duplicate elements in same array
Not optimized solution
or you can use list
arr.stream().filter(x -> x == 1).toList();
😢wah bhi
Use distinct
What if all numbers were same let's say 1
Good
Bc 🔥🔥
It will fail for some edge cases
Ye s like if in the last if u have one more 1
Data structure sikh pehle