Yapa Yapa - 9 months ago 29
Java Question

Find duplicates from an array using one for loop in java

I want to find duplicates from an array by using only one for loop. I did it by using two for loops. How can I do this in Java?

duplicates = false;
for (int i = 0; i < array.length; i++){
for(int j = i+1; j < array.length; j++){
duplicates = true;

Answer Source

You might consider to use Set, Otherwise You will probably need to do two loops. The below sample won't have a duplicates. Remember that I am not giving you a guarantee that this option has higher performance. performance wise it is better to work in raw arrays than using java collection apis'

Set  setString = new LinkedHashSet();

    for(int i=0;i<array.length;i++){