Search

Saturday, October 31, 2015

2 Ways to find duplicate elements in an Array - Java

Problem: You have given an array of objects, which could be array of integers and or array of Strings or any object which implements Comparable interface. How would you find duplicate elements from array? Can you solve this problem in O(n) complexity? This is actually one of the frequently asked coding problems from Java interviews. There are multiple ways to solve this problem and you will learn two popular ways here, first the brute force way, which involves comparing each element with every other element and other which uses a hash table like data structure to reduce the time complexity of problem from quadratic to liner, of course by trading off some space complexity. This also shows that how by using a suitable data structure you can come up with better algorithm to solve a problem. If you are preparing for programming job interviews, then I also suggest you to take a look at Cracking the Coding Interview book, which contains 150 programming questions and solutions, good enough to do well on any programming job interviews e.g. Java, C++, Python or Ruby.
Read more »

No comments:

Post a Comment