Question

/** Given an int array, return an int array with duplicate ints removed if the array...

/**
                Given an int array, return an int array with duplicate ints removed if the array contains duplicate values. <br> <br>

                removeDuplicateInts({3})  -> {3} <br>
                removeDuplicateInts({1, 2})  -> {1, 2} <br>
                removeDuplicateInts({7, 7})  -> {7} <br>
                removeDuplicateInts({1, 7, 1, 7, 1})  -> {1, 7} <br>
                removeDuplicateInts({1, 2, 3, 4, 5})  -> {1, 2, 3, 4, 5}) <br>
                removeDuplicateInts({1, 2, 3, 2, 4, 2, 5, 2})  -> {1, 2, 3, 4, 5} <br>
                @param numbers int[] an array of integers.
                @return int[] An array with the duplicates removed.
        **/
        public static int[] removeDuplicateInts(int[] numbers) {
                //your code here
                        return numbers;
                } // end removeDuplicateInts
0 0
Add a comment Improve this question Transcribed image text
Answer #1
public static int[] removeDuplicateInts(int[] numbers) {
    int uniqueLength = 0;
    boolean duplicateFound;
    for (int i = 0; i < numbers.length; ++i) {
        duplicateFound = false;
        for (int j = 0; j < i; ++j) {
            if (numbers[i] == numbers[j]) duplicateFound = true;
        }
        if (!duplicateFound) {
            uniqueLength++;
        }
    }
    int[] result = new int[uniqueLength];
    int ind = 0;
    for (int i = 0; i < numbers.length; ++i) {
        duplicateFound = false;
        for (int j = 0; j < i; ++j) {
            if (numbers[i] == numbers[j]) duplicateFound = true;
        }
        if (!duplicateFound) {
            result[ind++] = numbers[i];
        }
    }
    return result;
}
Add a comment
Know the answer?
Add Answer to:
/** Given an int array, return an int array with duplicate ints removed if the array...
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
  • /** Given an int array, return true if the array contains duplicate values. duplicateInts({3}) -> false...

    /** Given an int array, return true if the array contains duplicate values. duplicateInts({3}) -> false duplicateInts({1, 2}) -> false duplicateInts({7, 7}) -> true duplicateInts({1, 2, 3, 4, 5}) -> false duplicateInts({1, 2, 3, 2, 4, 5}) -> true **/ public static boolean duplicateInts(int[] numbers) { //your code here return false; }//end duplicateInts /** Given a String array, return true if the array contains duplicate values. Note: Capital letters count duplicateStrings({"a"}) -> false duplicateStrings({"a", "b", "c", "d"}) -> false duplicateStrings({"a",...

  • Hey, I was wondering if there’s another way to make these methods not reliable to imports...

    Hey, I was wondering if there’s another way to make these methods not reliable to imports such as “import java.util.Arrays” and “import java.util.Hash” I am still new in coding and would like to know if there’s another way to do it! Here is the code! (Thank you in advance and I will really appreciate it :) ) /** This exercise involves implementing several methods. Stubs for each method with documentation are given here. It is your task to fill out...

  • I need help with this Java exercise. I'm not allowed to use imports and I tried...

    I need help with this Java exercise. I'm not allowed to use imports and I tried coding it but ended up failing. /** Given a String array, return an String array with duplicate Strings removed if the array contains duplicate values. <br> Note: Capital letters count. <br> <br> removeDuplicateStrings({"a"}) -> {"a"} <br> removeDuplicateStrings({"a", "b", "c", "d"}) -> {"a", "b", "c", "d"} <br> removeDuplicateStrings({"a", "a"}) -> {"a"} <br> removeDuplicateStrings({"A", "a"}) -> {"A", "a"} <br> removeDuplicateStrings({"these", "are", "the", "times"}) -> {"these", "are",...

  • Consider a non-empty int array ints. A contiguous subarray ints[ start .. start + len -1...

    Consider a non-empty int array ints. A contiguous subarray ints[ start .. start + len -1 ] (with starting index start and length len) is called a flat if all elements of that subarray are equal. Furthermore, such a subarray is called a plateau if it is flat and each of the elements ints[start -1] and ints[start + len] that immediately proceed/succeed the subarray are either nonexistent (i.e., out of array’s index range) or are strictly smaller than the elements...

  • /**    Given a String and an array of two Strings,    return a three String...

    /**    Given a String and an array of two Strings,    return a three String array containing the strings in alphabetical order.    Note: Capital letters count    sort3Strings("wallace", {"washington", "irving"}) -> {"irving", "wallace", "washington"}    sort3Strings("wallace", {"washington", "Irving"}) -> {"Irving", "wallace", "washington"}    sort3Strings("Washington", {"irving", wallace"}) -> {"Washington", "irving", "wallace"}    sort3Strings("washington", {"washington", "Washington"}) -> {"Washington", "washington", "washington"} **/ public static String[] sort3Strings(String stringValue, String[] stringArray) {    //your code here    return new String[1]; }//end sort3Strings   ...

  • Which of the following are valid array declarations? a. int[] array- new int[10]; b. double [array...

    Which of the following are valid array declarations? a. int[] array- new int[10]; b. double [array double[10]; c. charl charArray "Computer Science"; None of the above Analyze the following code: class Test public static void main(Stringl] args) System.out.println(xMethod(10); public static int xMethod(int n) System.out.println("int"); return n; public static long xMethod(long n) System.out.,println("long"); return n The program displays int followed by 10 The program displays long followed by 10. The program does not compile. None of the above. tions 3-4 are...

  • Please write in C# Write a method in C# int[]GetDuplicatesIndices(int[]array), which takes an array of integers...

    Please write in C# Write a method in C# int[]GetDuplicatesIndices(int[]array), which takes an array of integers and returns an array that holds the indices of all the duplicates. Example if the array has the following value 10, 5, 3, 7, 5, 11, 15, 13, 11, 6, 3, 19 then note that the value 5 has duplicate at index 4, value 3 has a duplicate at index 11, and value 7 has a duplicate at index 6. So the method should...

  • C++ Language Given an array length 1 or more of ints, return the difference between the...

    C++ Language Given an array length 1 or more of ints, return the difference between the largest and smallest values in the array. Examples: largeDiff (410, 3, 5, 6}, 4) - 7 largeDiff ({7, 2, 10, 9}, 4) - 8 largeDiff ({2, 10, 7, 2}, 4) + 8 largeDiff ({2}, 1) = 0 code.cpp + New I Full Screen 2- int largeDiff (const int a[], int n) { 3 4 5}

  • I am unsure how to add the following methods onto this code?? please help - rowValuesIncrease(int[][]...

    I am unsure how to add the following methods onto this code?? please help - rowValuesIncrease(int[][] t) A method that returns true if from left to right in any row, the integers are increasing, otherwise false. - columnValuesIncrease(int[][] t) A method that returns true if from top to bottom in any column, the integers are increasing, otherwise false. - isSetOf1toN(int[][] t) A method that returns true if the set of integers used is {1, 2, . . . , n}...

  • The following code skeleton contains a number of uncompleted methods. With a partner, work to complete...

    The following code skeleton contains a number of uncompleted methods. With a partner, work to complete the method implementations so that the main method runs correctly: /** * DESCRIPTION OF PROGRAM HERE * @author YOUR NAME HERE * @author PARTNER NAME HERE * @version DATE HERE * */ import java.util.Arrays; import java.util.Random; import java.util.Scanner; public class ArrayExercises { /** * Given a random number generator and a length, create a new array of that * length and fill it from...

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