Question

Read 20 integers into an array. Next, use the unique algorithm to reduce the array to...

Read 20 integers into an array. Next, use the unique algorithm to reduce the array to the unique values entered by the user. Use the copy algorithm to display the unique values.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

public class RemoveDuplicatesArray {

    public static int[] removeDuplicates(int[] originalArray) {
        int uniqueLength = 0;
        boolean duplicateFound;
        for (int i = 0; i < originalArray.length; ++i) {
            duplicateFound = false;
            for (int j = 0; j < i; ++j) {
                if (originalArray[i] == originalArray[j]) duplicateFound = true;
            }
            if (!duplicateFound) {
                uniqueLength++;
            }
        }
        int[] result = new int[uniqueLength];
        int ind = 0;
        for (int i = 0; i < originalArray.length; ++i) {
            duplicateFound = false;
            for (int j = 0; j < i; ++j) {
                if (originalArray[i] == originalArray[j]) duplicateFound = true;
            }
            if (!duplicateFound) {
                result[ind++] = originalArray[i];
            }
        }
        return result;
    }

    public static void main(String[] args) {
        int[] arr = {6, 5, 6, 2, 4, 2, 7};
        int[] unique = removeDuplicates(arr);
        System.out.print("Array after removing duplicates is: ");
        for (int i = 0; i < unique.length; ++i) {
            System.out.print(unique[i] + " ");
        }
        System.out.println();
    }

}

Add a comment
Know the answer?
Add Answer to:
Read 20 integers into an array. Next, use the unique algorithm to reduce the array to...
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
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