2

I want to return duplicates in an array.

int[] strArray = new int[] {1,1, 2, 3, 2, 2, 3};

I have used below method to return duplicates.

private static Set<Integer> checkDuplicate(int[] intArray) {
    Set<Integer> values = new HashSet<>();

    for (int i = 0; i < intArray.length - 1; i++) {
        if (intArray[i] == (intArray[i + 1])) {
            values.add(intArray[i]);
        }

        else
            System.out.println("not equal");
    }

    return values;
}

But in this way it checks only the consequtive values.And this needs huge comparisons and time consuming. So is there any better way to do this?

1

6 Answers 6

3

If you do not want to use hashing (HashSet or HashMap), you can first sort the array. And then, you can find duplicates by checking consecutive values. Overall, this method has O(n log n) time complexity.

Sign up to request clarification or add additional context in comments.

Comments

2

You may try this example:

import java.util.*;
class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        int[] strArray = new int[] {1,1, 2, 3, 2, 2, 3, 4, 7, 5, 4};
        Set<Integer> duplicates = checkDuplicate(strArray);
        System.out.printf("Duplicates: %s\n", duplicates);
    }
    private static Set<Integer> checkDuplicate(int[] intArray)
    {
        Set<Integer> duplicates = new HashSet<Integer>();
        Set<Integer> tmp = new HashSet<Integer>();
        for(Integer i: intArray)
        {
            if(!tmp.add(i))
            {
                duplicates.add(i);
            }
        }
        return duplicates;
    }
}

Comments

1

If efficiency is what you are looking for then use HashMap that it has O(1) speed, Also iterate the array of integers in enhanced forloop because it is slightly faster than ordinary forloop

 private static Set<Integer> checkDuplicate(int[] intArray) {
    HashMap<Integer,Integer> values = new HashMap<Integer,Integer>();
    Set<Integer> values2 = new HashSet<Integer>();


    for(Integer i : intArray) //0(n)
    {
        if(values.get(i) != null) //O(1)
            values2.add(i);
        else
            values.put(i, i);
    }


    return values2;
}

Comments

0

Scan your input and store each number with it's count in a Map<Interger, Integer>. Then loop over the Map and put all keys with value>1 in the resulting Set

See also here: https://stackoverflow.com/a/15217535/461499

Comments

0

You must have to use Collections.frequency

It uses only equals method. If you use any collection Map,Set,List which uses equals method to compare two objects as well as Has collection uses hashCode methods which takes more processing time.

Comments

0
public static void main(String[] args) {
        int[] arr = { 1, 2, 3, 1, 4, 4, 1, 5 };
        // System.out.println(Arrays.toString(arr));
        List<Integer> l = new ArrayList<Integer>();
        for (int i = 0; i < arr.length; i++) {
            l.add(i, arr[i]);
        }
        // System.out.println(l);
        Set<Integer> set = new HashSet<Integer>();
        for (int j = 0; j < l.size(); j++) {
            if (Collections.frequency(l, l.get(j)) > 1) {
                set.add(l.get(j));
            }
        }

        System.out.println(set);

    }

O/P :
[1, 4]

3 Comments

This will just give count of each duplicate, not the value.
I want to return the values which has duplicates. Not the frequency of duplicates.
This has O(n^2) time complexity, which the OP is trying to avoid.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.