Question

use java code Write a collection of array utility methods. •Write a method to create a...

use java code Write a collection of array utility methods.

•Write a method to create a list of duplicate numbers in an array.

•Write a method to create a list of duplicate Strings in an array.

•Convert to using generics.

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

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

// ArrayUtilties.java

import java.util.ArrayList;

import java.util.List;

public class ArrayUtilties {

              /**

              * method to find the list of duplicate numbers in an array of integers

              */

              public static List<Integer> findDuplicateNumbers(int numbers[]) {

                            // creating a temporary list

                            ArrayList<Integer> tempList = new ArrayList<Integer>();

                            // creating a list to store duplicates

                            ArrayList<Integer> duplicates = new ArrayList<Integer>();

                            // looping through all elements in numbers

                            for (int i = 0; i < numbers.length; i++) {

                                          // checking if current number is already present in tempList and not

                                          // in duplicates.(i.e checking if current number is met before)

                                          if (tempList.contains(numbers[i])

                                                                      && !duplicates.contains(numbers[i])) {

                                                        //adding to duplicates list

                                                        duplicates.add(numbers[i]);

                                          } else {

                                                        //adding other numbers to tempList

                                                        tempList.add(numbers[i]);

                                          }

                            }

                            //returning list of duplicates

                            return duplicates;

              }

              /**

              * method to find the list of duplicate strings in an array of strings

              */

              public static List<String> findDuplicateStrings(String strings[]) {

                            ArrayList<String> tempList = new ArrayList<String>();

                            ArrayList<String> duplicates = new ArrayList<String>();

                            for (int i = 0; i < strings.length; i++) {

                                          if (tempList.contains(strings[i])

                                                                      && !duplicates.contains(strings[i])) {

                                                        duplicates.add(strings[i]);

                                          } else {

                                                        tempList.add(strings[i]);

                                          }

                            }

                            return duplicates;

              }

              /**

              * method to find and return a generic list of duplicate elements in a

              * generic array. Here type T can be any datatype

              */

              public static <T> List<T> findDuplicates(T array[]) {

                            ArrayList<T> tempList = new ArrayList<T>();

                            ArrayList<T> duplicates = new ArrayList<T>();

                            for (int i = 0; i < array.length; i++) {

                                          if (tempList.contains(array[i]) && !duplicates.contains(array[i])) {

                                                        duplicates.add(array[i]);

                                          } else {

                                                        tempList.add(array[i]);

                                          }

                            }

                            return duplicates;

              }

              public static void main(String[] args) {

                            // testing all three implementations

                            int numbers[] = { 10, 10, 10, 15, 1, 9, 8, 7, 66, 11, 1 };

                            System.out.println(findDuplicateNumbers(numbers));

                            String strings[] = { "oliver", "john", "felicity", "john", "barry" };

                            System.out.println(findDuplicateStrings(strings));

                            Double doubles[] = { 22.5, 66.6, 10.55, 22.5, 10.5, 66.6 };

                            System.out.println(findDuplicates(doubles));

              }

}

/*OUTPUT*/

[10, 1]

[john]

[22.5, 66.6]

Add a comment
Know the answer?
Add Answer to:
use java code Write a collection of array utility methods. •Write a method to create a...
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