Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a function that takes an array of integers and its size as parameters and

ID: 3915270 • Letter: W

Question

Write a function that takes an array of integers and its size as parameters and changes all the duplicate values to -1. As the sample below shows, the first occurrence of each value is left unchanged whereas later occurrences of the same value are changed to -1.

PLZ WRITE IN C LANGUAGE

Write a function that takes an array of integers and its size as parameters and changes all the duplicate values to 1. As the sample below shows, the first occurrence of each value is left unchanged whereas later occurrences of the same value are changed to -1 Array data 1 2 45 3 21 5 6 4 After processing

Explanation / Answer

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. Thanks

//Code

#include<stdio.h>

//method to replace duplicate elements with -1 in an array

void replaceDuplicates(int* array, int size){

                //looping through all elements

                for(int i=0;i<size;i++){

                                //a flag variable to denote if duplicate is found

                                int duplicate_found=0;

                                //checking all previous elements

                                for(int j=0;j<i;j++){

                                                //checking if current element occured before

                                                if(array[j]==array[i]){

                                                                //duplicate found

                                                                duplicate_found=1;

                                                                break;

                                                }

                                }

                                //if duplicate found, changing element to -1

                                if(duplicate_found==1){

                                                array[i]=-1;

                                }

                }

}

int main(){

                //creating an array

                int array[]={1,2,4,5,3,2,1,5,6,4};

                int size=10;

                //displaying original array

                printf("Original array: ");

                for(int i=0;i<size;i++){

                                printf("%d ",array[i]);

                }

                printf(" ");

                //replacing dupllicates with -1

                replaceDuplicates(array, size);

                //displaying modified array

                printf("After replacing duplicates with -1: ");

                for(int i=0;i<size;i++){

                                printf("%d ",array[i]);

                }

                printf(" ");

}

/*OUTPUT*/

Original array:

1 2 4 5 3 2 1 5 6 4

After replacing duplicates with -1:

1 2 4 5 3 -1 -1 -1 6 -1

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote