Recursion to compare two Arrays without using loop [pseudo code]

how to compare two arrays in java using for loop
how to compare elements of two arrays in java
how to compare two string arrays in java
how to compare two arrays in c++
how to compare two arrays of different size in java
compare array elements in c
how to compare two arrays in javascript
check if two arrays contain same elements c#

Consider the given two arrays A and B without repetitions (that is, no double occurrences of the same element). The task is to check whether each element of B is also an element of A without regard to the order. For instance if A = [1, 2, 3, 4] and B = [2, 3, 1] then the answer is YES. If however B = [1, 2, 5] then the answer is NO because 5 is not in A.

Design a recursive algorithm (no use of loops) for the above problem.

I am trying to solve the above problem, and there is no way I can find to solve it without using the loop. Would anyone know a way to solve this with recursion without using a loop?

I can not use any builtin functions, this is a recursion exercise for algorithms and data structures.

You can convert a loop to recursion by designing a recursive function/method that operates on a part of your original array (technically on a sub-array) (initially the sub-array will be your complete array) and reducing the size of the array (each time you are passing to your recursive method) by 1.

The following method (I've the implementation in Java) simply checks if the given number is present in the array/list. But notice that it also takes startIndex and endIndex which specifically denotes our boundaries of sub-array/sub-list.

In simple words the following method checks whether the given number is present in list or not but the check is done only between startIndex and endIndex both inclusive. Consider that you pass each element of your array B (listB in my case) to this method, and list argument is actually a reference to your array A (listA in my case).

 * This method recursively checks whether given
 * number is contained in the given list or not.
 * For this method startIndex and endIndex
 * correspond to the indices of listA
private static boolean contains(List<Integer> list, int startIndex, int endIndex, int number) {
    if (startIndex == endIndex) {
        return list.get(startIndex) == number;
    } else if (startIndex < endIndex) {
        return list.get(startIndex) == number || contains(list, startIndex + 1, endIndex, number);
    // should never be the case
    return true;

Now, once you have the above method, you can now device a recursive method that pics up all the elements of listB one at a time, and "plugs it in" inside the above method. This can be preciously done as follows:

 * This method recurse over each element of listB and checks
 * whether the current element is contained in listA or not
 * for this method startIndex and endIndex correspond to the
 * indices of listB
private static boolean contains(List<Integer> listA, List<Integer> listB, int startIndex, int endIndex) {
    if (startIndex > endIndex) {
        return true;
    boolean c = contains(listA, 0, listA.size() - 1, listB.get(startIndex));
    if (!c) {
        return false;
    return contains(listA, listB, startIndex + 1, endIndex);

And a call to above method will look like contains(listA, listB, 0, listB.size() - 1) Bingo!! You are done.

I'd like you to think of recursive functions in a specific manner. Think of them as like what arguments it take, and what it does. So when you have a recursive call inside the recursive method, you don't need to think how that recursive call will work, rather have an abstraction there and believe that the recursive call will give you the result. Now focus on how you can use this returned result to make this recursive method correctly work.

Recursion to compare two Arrays without using loop [pseudo code], Recursion to compare two Arrays without using loop [pseudo code] Consider the given two arrays A and B without repetitions (that is, no double occurrences of the same element). The task is to check whether each element of B is also an element of A without regard to the order. Your logic here is wrong. You can't directly compare arrays like this. It's comparing the memory address. This will always be false unless you pass through the exact same array when the method is called - which i don't think is what you're trying to do . Array lengths are set manually, so it's a conscious effort to get a difference.

Use PowerShell to compare arrays, at once. If the SideIndicator property is => , this means the InputObject property returned is in the DifferenceObject value and not in the ReferenceObject value and vice versa for the <= SideIndicator . Therefor I need to both scan in and compare values in the array without using a for loop. I suppose the other option is to not loop with the case value, thereby enabling me to use a loop elsewhere and keeping my big-oh run time below n. If anybody can help me, I would appreciate it.

Pseudocode for a function that recursively tests whether the array A contains a given element, x might look something like this:

function isMember(x, A):
    if A = [] then return false

    if x = A[0] then return true

    return isMember(x, A[1..-1])

This function is built on the premise that to test if x is a member of A, we can test to see if the first element of A, denoted A[0], is the same as x. If so, we can terminate the function and return true. If not, then call the function again, passing it all the elements of array A except the first one that we already tested. I've denoted the remaining elements of A with A[1..-1], i.e. element numbers 1 through to -1, which in some languages is another way to refer to the last element.

Now during the second call to this function, x is being compared to the first element of A[1..-1], which is, of course, the second element of A. This recurses over and over, each time shrinking the size of array A the element at the top of the list is tested and discarded.

Then we eventually reach the final case, where there are no more elements left in A to test, which results in the final recursive call to the function being passed an empty array. In this situation, we can infer that every element in A failed to match with x, and so we can safely return false, stating the x is not a member of A.

Now, in order to determine whether a given array B is contained by array A, each element in B needs to undergo the test described above. If isMember() returns true for every element in B, then B must be contained by A. If any one element causes isMember() to return false, then we can stop further testing because B contains elements that are not in A, so it cannot be a subarray.

Here's some pseudocode that illustrates this recursive process:

function containedBy(B, A):
    if B = [] then return true

    let x := B[0]
    if not isMember(x, A) then return false

    return containedBy(B[1..-1], A)

It's very similar in many ways to the first function, which isn't surprising. This time, however, array B is reduced in size with each recursion, as its lead element is passed through to the isMember() function, then discarded upon isMember() returning true. As before, the final case after the last recursive call to containedBy() passes through an empty list in place of B. This can only mean that every element of B successfully passed the membership test with A, so we return true to confirm that B is, indeed, contained by A.

How to know if two arrays have the same values, How do you check if two arrays are equal in typescript? A simple way is to run a loop and compare elements one by one. Java provides a direct method Arrays.equals() to compare two arrays. Actually, there is a list of equals() methods in Arrays class for different primitive types (int, char, ..etc) and one for Object type (which is base of all classes in Java).

Check if two arrays are equal or not, evaluates to true . var sourceArray = [1, 2, 3]; var targetArray = [3, 2, 1]; if (sourceArray. length !== A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

How to compare two arrays in Java?, Given two given arrays of equal length, the task is to find if given arrays are equal or not. arr1[] = {1, 2, 5, 4, 0, 2, 1}; arr2[] = {2, 4, 5, 0, 1, 1, 2}; Output : Yes Input : arr1[] = {1, 7, 1}; arr2[] = {7, 7, 1}; Output : No A simple solution is to sort both array and then linearly compare elements. C++ program to find given two array​. Given an array, find all unique subsets with a given sum with allowed repeated digits. Check if given number is Prime - O(√N) Solution – Java Program; Find all Prime Numbers less than equal to N | Set 1; Given an array, print all unique subsets with a given sum. Swap two numbers without using extra variable; Given an array, Print sum of all

Check if 2 arrays have (exactly) the same elements recursively , A simple way is to run a loop and compare elements one by one. Java provides a direct method Arrays.equals() to compare two arrays. If not, this method will then proceed to compare the objects field by field, recursively. and it will never enter into an endless loop (Source: Recursion . Recursion means "defining a problem in terms of itself". This can be a very powerful tool in writing algorithms. Recursion comes directly from Mathematics, where there are many examples of expressions written in terms of themselves. For example, the Fibonacci sequence is defined as: F(i) = F(i-1) + F(i-2)

  • I have done it using nested loops. That was easy, but I do not know how to do it using recursion WITHOUT using loop...
  • You question will be down-voted a lot ... SO is not a do my problem type of a site. It is more of help me figure out what is wrong with my code type of an idea.
  • oh ok, I just joined the site.
  • You can recursively iterate and put all elements of A in a map. Now, recursively iterate over B and check if each element exists in map or not. If not, return false, else true.
  • How would I write pseudocode for map? I am sorry for asking so basic question.