Search code examples
javaarraysparallel-processingparallel-arrays

How do I copy elements in a parallel array into a new parallel array with no duplicates in Java?


Hi guys I'm having a problem getting the right output for an assignment. I am looking for a way to copy a set of elements from one parallel array (which contains a string--int) to the other with no duplicate value. For example: I have these set of parallel arrays

This is the ORIGINAL PARALLEL ARRAY :

String[] phoneNumbers;           
    phoneNumbers = new String[100];
    int[] callDurations = new int[phoneNumbers.length];
    int size = 0;

    phoneNumbers[0] = "888-555-0000";
    callDurations[0] = 10;
    phoneNumbers[1] = "888-555-1234";
    callDurations[1] = 26;
    phoneNumbers[2] = "888-555-1234";
    callDurations[2] = 2;
    size = 3; 

I want to create a method that creates new pairs of parallel arrays from the original arrays (phoneNumber & callDuration).This method would be called totalDuration and it would return no value(void). It would check if a number from the current array is in the new array, if yes, it will just add any duplicate duration to the current duration. if no, it would add a new element to the NewNumber array and add an element to the NewDuration array.

public static int find(String[] list, int size, int start, String target) {
    int pos = start;

    while (pos < size && !target.equals(list[pos])) {
        pos++;
    }

    if (pos == size)
        pos = -1;

    return pos;
}

This find method would be used to check if a phone number has already been placed in the new arrays, and if so, to determine where that number is.

For example, if the array contains

    phoneNumbers[0] = "888-555-0000";
    callDurations[0] = 10;
    phoneNumbers[1] = "888-555-1234";
    callDurations[1] = 26;
    phoneNumbers[2] = "888-555-1234";
    callDurations[2] = 2;

printing calls details for "888-555-1234" would look like:

    all calls from: 
Calls from 888-555-1234:
888-555-1234 duration: 26s
888-555-1234 duration: 2s

the output for the new method instead should be(26s +2s):

    all calls from: 
Calls from 888-555-1234:
888-555-1234 duration: 28s

I tried to solve it with this code but its giving a wrong output:

public static void totalDurations(String[] phoneNumbers, int[] callDuration, int size, String target) {
    String[] NewNumbers;
    int[] NewDuration;
    int pos;

    NewNumbers = new String[phoneNumbers.length];
    NewDuration = new int[callDuration.length];
    pos = find(phoneNumbers,size, 0,target);
    while(pos < size && !target.equals(phoneNumbers[pos])) {
        NewNumbers[pos] = phoneNumbers[pos];
        NewDuration[pos] = callDuration[pos];
        System.out.println(NewNumbers[pos] + "duration" + NewDuration[pos] +"s");
    }

}

NOT-RELATED The code I use to get all my details for each call is my method "findAllCalls"

public static void findAllCalls(String[] phoneNumbers, int[] callDurations, int size, String targetNumber) {
    int matchPos;

    System.out.println("Calls from " + targetNumber + ":");
    matchPos = find(phoneNumbers, size, 0, targetNumber);
    while (matchPos >= 0) {
        System.out.println(phoneNumbers[matchPos] + " duration: " + callDurations[matchPos] + "s");


        matchPos = find(phoneNumbers, size, matchPos + 1, targetNumber);
    }
}

System.out.println("\n all calls from: ");
    findAllCalls(phoneNumbers,callDurations,size,"888-555-1234");

Any correction would be much appreciated in advance.


Solution

  • Well according to your question the method for printing total duration of a target number can be like following

        public static int numberIndex(String[] numbers, String target) {
            for(int i = 0; i < numbers.length; i++) {
                if(numbers[i].equals(target)) {
                    return i;
                }
            }
    
            return -1;
        }
    
        public static void totalDuration(String[] phoneNumbers, int[] callDurations, String target) {
            String[] newNumbers = new String[phoneNumbers.length];
            int[] newDurations = new int[callDurations.length];
            int newIndex = 0;
    
            for(int i = 0; i < phoneNumbers.length; i++) {
                int oldIndex = numberIndex(newNumbers, phoneNumbers[i]);
    
                if(oldIndex == -1) {
                    newNumbers[newIndex] = phoneNumbers[i];
                    newDurations[newIndex] = callDurations[i];
                    newIndex++;
                }
                else {
                    newDurations[oldIndex] += callDurations[i];
                }
            }
    
            for(int i = 0; i < newIndex; i++) {
                System.out.println("Total duration for " + newNumbers[i] + ": " + newDurations[i]);
            }
        }
    

    As you mentioned that the return type of this method should be void, so I assumed you only need to print the total duration. For that there is no need to construct new arrays like NewNumbers or NewDurations.

    If holding them in arrays is absolutely necessary, let me know in the comment.