-3

Can anyone help me to provide the code for finding duplicate values from the array. Here the condition is no loop statements. I tried with recursion method but it not working out. Pls anyone help me on this.

My attempt:

public static void main(String[] args) {
    Integer a[] = { 1, 2, 7, 3, 4, 5, 2, 7 };
    ArrayList<Integer> al = new ArrayList<Integer>(Arrays.asList(a));
    if (duplicate(al) == true) {
        System.out.println("Duplicate");
    }
}

static int i = 1;

private static boolean duplicate(ArrayList<Integer> al) {

    if (al.get(i) != null) {
        if (al.get(i - 1) == al.get(i)) {
            System.out.println("Duplicate are : " + al.get(i));
        }
    } else {
        return true;
    }
    i++;
    return duplicate(al);
}
Ashok
  • 115
  • 3
  • 8
  • Could you show your attempts? – Andrew Li Nov 21 '16 at 05:41
  • Who put the ridiculous "no loops" condition on this problem? Were they expecting you to use recursion, or were they thinking there were built-in library methods that could do the whole thing, or maybe streams? Are you allowed to use Sets? Is there anything else you aren't allowed to use? This looks more like a puzzle rather than a programming problem or learning exercise. – ajb Nov 21 '16 at 05:53
  • Also, what's the expected output? Is it just supposed to be "yes" or "no" to say whether there are duplicates, or is it supposed to find the duplicates? – ajb Nov 21 '16 at 05:54
  • From the method signature OP uses, he probably just want to check whether duplicates exist. I agree the no-loops condition is kind of weird. – user3437460 Nov 21 '16 at 05:58
  • The objective of the program is to display the duplicate values. I might be done some mistake in the return value. I stuck on the logic hence I didn't correct it. – Ashok Nov 21 '16 at 06:07
  • You may want to take a look at [this thread](http://stackoverflow.com/questions/14944458/find-duplicate-element-in-array-in-time-on). Your question is a possible duplicate of that. – Shyam Baitmangalkar Nov 22 '16 at 08:01

4 Answers4

1

Please refer below code to find duplicates in array without loop

public class ArrayDuplicate {

public static void main(String[] args) {

    findDuplicateElementsInArray(new int[] { 20, 10, 20, 5, 10 });

}

private static void findDuplicateElementsInArray(int arr[]) {

    Set<Integer> uniqueElements = new LinkedHashSet<Integer>();

    Arrays.stream(arr).filter(i -> !uniqueElements.add(i)).forEach(System.out::println);

}

}

0

This isn't a good use case for recursion, which leads me to wonder what the point of the problem is. However, it's worth noting that LISP programmers (from what I've observed) traditionally used recursion for everything; early versions of the language may not have had any kind of loop construct. When programming in this way, one gets used to figuring out how to use recursion for an algorithm that would be a loop in any other language.

The main technique, I think, is to figure out what running local variables you'll need to keep, and pass them as parameters to a recursive helper function.

To solve this problem with a loop, I'd define a Set that is initially empty. As I go through the array, I'd: (1) see if the array element is already in the set, and return true if it is; (2) add the element to the set.

Here, the Set is the running variable that you need to keep. The array index is another "running variable". (In classic LISP, you'd just use a cdr function that means "the rest of the list", so you wouldn't need to maintain an index; I don't think that's easy to do with a Java ArrayList.) So you'll want a recursive method that has a Set and a "current index" as a parameter:

private boolean hasDuplicateHelper(ArrayList<Integer> a, int currentIndex, Set<Integer> alreadySeen) { ... }

The outer method will initialize the set to an empty set and call the helper with it this set, and with 0 as the current index. The recursive method will (1) look at the current element and see if it's in alreadySeen and return true if it is; (2) add the current element to the set; (3) call the method recursively with the new set as the alreadySeen parameter, and with the appropriate new value for the current index (I'll let you figure that one out).

I'll leave it to you to work out the rest of the details, such as how to stop.

EDIT: Now that it's clear from the comments that the desired result is to print the duplicate values instead of just "yes" or "no", something will have to change. But I think this can be done just by changing the method result to a Set<Integer> that contains a set of all the duplicates. If more information is needed, such as the indexes where the duplicates occur or the number of times each duplicate occurs, a different result structure may have to be used.

ajb
  • 31,309
  • 3
  • 58
  • 84
0

I completed the code to find the duplicate number from the array without using the loop statement. I implemented two recursions to finalize the code. Please check code on below

static int size;

public static void main(String[] args) {

    Integer a[] = { 2, 10, 6, 1, 2, 4, 6, 10, 1 };
    ArrayList<Integer> al = new ArrayList<Integer>(Arrays.asList(a));
    size = a.length - 1;
    findDuplOne(al, 0);
}

static int i = 0;

private static void findDuplOne(ArrayList<Integer> al, int i) {

    if (i <= size) {
        int valOne = al.get(i);
        i++;
        findDuplTwo(al, i, valOne);
        findDuplOne(al, i);
    }
}

private static void findDuplTwo(ArrayList<Integer> al, int i, int compareVal) {
    if (i <= size) {
        int valOne = al.get(i);
        if (compareVal == valOne) {
            System.out.println("Duplicate is " + compareVal);
        }
        i++;
        findDuplTwo(al, i, compareVal);
    }
}
Ashok
  • 115
  • 3
  • 8
0

Simply you can try it. If you pass your array object then it will return the count of duplicates.

    public static int findDuplicatesCountOnArray(Integer[] arr) {
    List<Integer> list = new ArrayList<Integer>();
    if (arr != null) {
        list.addAll(Arrays.asList(arr));
    }
    HashSet<Integer> set = new HashSet<Integer>(list);
    return list.size() - set.size();
}
Ganesa Vijayakumar
  • 2,422
  • 5
  • 28
  • 41