Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: ArrayDiff Problem on CodeWars; Need Assistance

  1. #1
    Junior Member
    Join Date
    May 2021
    Location
    Alberta
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question ArrayDiff Problem on CodeWars; Need Assistance

    I am trying to solve the ArrayDiff challenge on CodeWars with the Java language. I've already completed Javascript and Python, but I'm less familiar with Java and I cannot figure it out. Ive already asked for help on that site but I've not received a response yet.
    Here's the problem:

    "Your goal in this kata is to implement a difference function, which subtracts one list from another and returns the result.

    It should remove all values from list a, which are present in list b keeping their order.

    array_diff({1, 2}, 2, {1}, 1, *z) == {2} (z == 1)
    If a value is present in b, all of its occurrences must be removed from the other:

    array_diff({1, 2, 2, 2, 3}, 5, {2}, 1, *z) == {1, 3} (z == 2)"

    First off, why does it include a value for the arguments length after the argument ({1,2}, 2)?
    Why does it include *z? What does that mean? This is different from the JS and Python questions.
    Anyways, here is my makeshift attempt at code:

    public class Kata {
     
      public static int[] arrayDiff(int[] a, int[] b) {
        HashSet<Integer> result = new HashSet<>();
        Arrays.sort(b);
        for (int elem : a) {
          if (Arrays.binarySearch(b, elem) < 0)
            result.add(elem);
        }
        int[] arr = new int[result.size()];
        int i = 0;
        for (int elem : result) {
          arr[i++] = elem;
        }
        return arr;
      }
     
    }
    Last edited by ryandc; May 8th, 2021 at 04:54 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ArrayDiff Problem on CodeWars; Need Assistance

    why does it include a value for the arguments length after the argument ({1,2}, 2)?
    Why does it include *z? What does that mean?
    I do not recognize either of the questions to be about a part of the java language. You need to talk with whoever posted the question to get definitions for the contents of those arrays.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    ryandc (May 8th, 2021)

  4. #3
    Junior Member
    Join Date
    May 2021
    Location
    Alberta
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Red face Re: ArrayDiff Problem on CodeWars; Need Assistance

    Sorry about that. New to this forum.

    To be honest those questions about what is in the arguments isn't really the issue.
    But I will try posting another question on Codewars and see if I can get some clarification.

    I'm just wondering, how do I write code for this problem in Java?
    I just need to return array a with all values present in array b removed.

    Is it true all arrays in Java are immutable?
    Can I add to an empty array that's dynamic (not a fixed size) in Java?

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ArrayDiff Problem on CodeWars; Need Assistance

    arrays in Java are immutable?
    The size can not be changed. The contents can be changed.

    Can I add to an empty array that's dynamic (not a fixed size) in Java?
    An array's size is fixed when it is declared. If you need dynamic then use a class like ArrayList.
    Or make a big temp array, add to it, then create another array of desired size and copy from temp to new array.

    how do I write code for this problem in Java?
    First make a design/algorithm for the program and then write code to implement it.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    ryandc (May 8th, 2021)

  7. #5
    Junior Member
    Join Date
    May 2021
    Location
    Alberta
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question Re: ArrayDiff Problem on CodeWars; Need Assistance

    For example, would this code be a possible solution?
     
    public class Kata {
     
      public static int[] arrayDiff(int[] a, int[] b) {
        HashSet<Integer> result = new HashSet<>();
        Arrays.sort(b);
        for (int elem : a) {
          if (Arrays.binarySearch(b, elem) < 0)
            result.add(elem);
        }
        int[] arr = new int[result.size()];
        int i = 0;
        for (int elem : result) {
          arr[i++] = elem;
        }
        return arr;
      }
     
    }

    Or could I use an ArrayList to append to instead? Would an ArrayList be better than a HashSet?
    You need to import the java.util package to access ArrayLists right?

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ArrayDiff Problem on CodeWars; Need Assistance

    would this code be a possible solution?
    Have you compiled and executed the code to test if it works?

    could I use an ArrayList to append to instead? Would an ArrayList be better than a HashSet?
    Sorry, I can't tell because I don't see any comments in the code describing what it is trying to do.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #7
    Junior Member
    Join Date
    May 2021
    Location
    Alberta
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: ArrayDiff Problem on CodeWars; Need Assistance

    I've adjusted my code to use ArrayList instead of HashMap.
    I've included comments to explain what is happening.

    I've tried executing the code in the Java Tutor at Java Tutor - Visualize Java code execution to learn Java online
    It produces an error once it reaches "result.add(elem);"

    import java.util.ArrayList;
    import java.util.Arrays;
    public class Kata {
     
      public static int[] arrayDiff(int[] a, int[] b) {
    // Initialize ArrayList to add elements from int[] a 
        ArrayList<String> result = new ArrayList<String>();
    // sort int[] b in order to binarySearch it for any element present in int[] a
        Arrays.sort(b);
    // If an element is present in a but is NOT present in b then add to result ArrayList
        for (int elem : a) {
          if (Arrays.binarySearch(b, elem) < 0)
            result.add(elem); // error occurs here
        }
    // Initialize integer array to transfer elements from result ArrayList
        int[] arr = new int[result.size()];
        int i = 0;
        for (int elem : result) {
          arr[i++] = elem;
        }
        return arr;
      }
     
    // I don't know how to execute the code in Java
    // Is this how you call the function?
    }
    public static int[] main(int[] args) {
        arrayDiff({1, 2}, 2, {1}, 1, *z);
      }

    I tried executing the code but it produces this message:

    Error: no suitable method found for add(int)
    method java.util.Collection.add(java.lang.String) is not applicable
    (argument mismatch; int cannot be converted to java.lang.String)
    method java.util.List.add(java.lang.String) is not applicable
    (argument mismatch; int cannot be converted to java.lang.String)
    method java.util.AbstractCollection.add(java.lang.String) is not applicable
    (argument mismatch; int cannot be converted to java.lang.String)
    method java.util.AbstractList.add(java.lang.String) is not applicable
    (argument mismatch; int cannot be converted to java.lang.String)
    method java.util.ArrayList.add(java.lang.String) is not applicable
    (argument mismatch; int cannot be converted to java.lang.String)

    --- Update ---

    I solved it!
    My solution works in CodeWars.
    For some reason it wasn't executing in the Java Tutor

    Here is the final solution I used:
    import java.util.ArrayList;
    import java.util.Arrays;
    public class Kata {
     
      public static int[] arrayDiff(int[] a, int[] b) {
        ArrayList<Integer> result = new ArrayList<>();
        Arrays.sort(b);
        for (int elem : a) {
          if (Arrays.binarySearch(b, elem) < 0)
            result.add(elem);
        }
        int[] arr = new int[result.size()];
        int i = 0;
        for (int elem : result) {
          arr[i++] = elem;
        }
        return arr;
      }
    }

Similar Threads

  1. Need some assistance with the method
    By awasthisa in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 21st, 2020, 01:28 PM
  2. Need any assistance
    By Chin24 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 16th, 2018, 01:21 PM
  3. In need of assistance!
    By Nexcit in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 11th, 2014, 05:24 AM
  4. [SOLVED] for loop assistance
    By Kseidel in forum Loops & Control Statements
    Replies: 3
    Last Post: September 17th, 2012, 08:10 PM
  5. Need some assistance please
    By JavaPhish in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 25th, 2012, 10:00 AM

Tags for this Thread