Question

Java: Write a class ArrayIntersection that implements the method below. public static int[] get(int[] one, int[]...

Java:

Write a class ArrayIntersection that implements the method below.

public static int[] get(int[] one, int[] two)

Given two arrays, it returns a new array with all values that are in both arrays.

The returned array has a size fitting its elements exactly and without duplicates. The order of values does not matter. For example, given {1,5,5,1} and {5,3}, the method returns {5}.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
public class ArrayIntersection {

    public static int[] get(int[] one, int[] two) {
        int count = 0;
        boolean found;
        for (int i = 0; i < one.length; i++) {
            found = false;
            for (int j = 0; j < two.length; j++) {
                if (one[i] == two[j]) {
                    found = true;
                }
            }
            if (found) {
                found = false;
                for (int j = 0; j < i; j++) {
                    if (one[i] == one[j]) {
                        found = true;
                    }
                }
                if (!found) {
                    ++count;
                }
            }
        }
        int[] result = new int[count];
        int index = 0;
        for (int i = 0; i < one.length; i++) {
            found = false;
            for (int j = 0; j < two.length; j++) {
                if (one[i] == two[j]) {
                    found = true;
                }
            }
            if (found) {
                found = false;
                for (int j = 0; j < i; j++) {
                    if (one[i] == one[j]) {
                        found = true;
                    }
                }
                if (!found) {
                    result[index++] = one[i];
                }
            }
        }
        return result;
    }

    public static void main(String[] args) {
        int[] one = {1, 5, 5, 1};
        int[] two = {5, 3};
        int[] result = get(one, two);
        for (int i = 0; i < result.length; i++) {
            System.out.print(result[i] + " ");
        }
        System.out.println();
    }
}

Add a comment
Know the answer?
Add Answer to:
Java: Write a class ArrayIntersection that implements the method below. public static int[] get(int[] one, int[]...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 5. Given the following class definition, write Java code that implements the addFirst method public class...

    5. Given the following class definition, write Java code that implements the addFirst method public class MyLinkedList<E> { private Node<E> head, tail; private int size 0; // Number of elements in the list /** Add an element to the beginning of the list */ public void addFirst (E e) { private static class Node<E> { E element; Node<E> next; public Node (E element) { = element; this.element

  • public static int[] interleaveArray(int[] a, int[] b) Given two arrays, return the array which interleaves the...

    public static int[] interleaveArray(int[] a, int[] b) Given two arrays, return the array which interleaves the elements of the two arrays. The two arrays do not have to be the same length. For example, given a = [1, 2, 3] and b = [4, 5, 6, 7, 8], the method returns c = [1, 4, 2, 5, 3, 6, 7, 8] Parameters: a - given array b - given array Returns: the array which interleaves the elements of the two...

  • Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements...

    Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements DoubleEndedList { private Node front; // first node in list private Node rear; // last node in list private int size; // number of elements in list ////////////////////////////////////////////////// // YOU MUST IMPLEMENT THE LOCATE METHOD BELOW // ////////////////////////////////////////////////// /** * Returns the position of the node containing the given value, where * the front node is at position zero and the rear node is...

  • Consider the following class: public class Sequence { private int[] values; public Sequence(int size) { values...

    Consider the following class: public class Sequence { private int[] values; public Sequence(int size) { values = new int[size]; } public void set(int i, int n) { values[i] = n; } public int get(int i) { return values[i]; } public int size() { return values.length; } } Add a method public boolean sameValues(Sequence other) to the Sequence class that checks whether two sequences have the same values in some order, ignoring duplicates. For example, the two sequences 1 4 9...

  • 5. (40 Points) Write a complete Java class named ArrayMethods that implements the methods listed below....

    5. (40 Points) Write a complete Java class named ArrayMethods that implements the methods listed below. All the methods accept the following parameters: Parameters: intar An array of int values. int firstIndex The index of the first element to include in the sum. int lastIndex The index of the last element to include in the sum. Please note that all methods must verify the validity of first Index and lastIndex. public static int sum(int[] arr, int first Index, int lastIndex)...

  • 1.Write code for a Java method, printArray, that takes an int array as parameter and prints...

    1.Write code for a Java method, printArray, that takes an int array as parameter and prints it out, one element per line. public static void printArray (int[] values){ Methods that do NOT return a result have “void” as return type. Notice how an array parameter type is passed, int [] }// end printArray 2.Write code for a Java method that takes in as input parameter an integer number, say num, and returns a double array of size num with all...

  • Java question Write the method reversed that returns true if and only if the arrays a...

    Java question Write the method reversed that returns true if and only if the arrays a and b contain exactly the same elements, but in reversed order. For example, reversed ({3, 1}, {1, 3}) returns true, but reversed ({3, 1}, {2, 3}) and reversed ({3, 1}, {1, 1, 3}) both return false. public static boolean reversed (int [] a, int [] b) should return true if and only if a and b contain the same elements, reversed.

  • Programming 5_1: Create a Java class named <YourName>5_1 In this class create a main method...

    Programming 5_1: Create a Java class named <YourName>5_1 In this class create a main method, but for now leave it empty. Then write a Java method getAverage which takes an array of integers as it’s argument. The method calculate the average of all the elements in the array, and returns that average. The method should work for an array of integers of any length greater than or equal to one. The method signature is shown below: public static double getAverage(...

  • JAVA - Circular Doubly Linked List Does anybody could help me with this method below(previous)? public...

    JAVA - Circular Doubly Linked List Does anybody could help me with this method below(previous)? public E previous() { // Returns the previous Element return null; } Explanation: We have this class with these two implemented inferfaces: The interfaces are: package edu.ics211.h04; /** * Interface for a List211. * * @author Cam Moore * @param the generic type of the Lists. */ public interface IList211 { /** * Gets the item at the given index. * @param index the index....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT