Question

In: Computer Science

in C programming language Write a function removeDups that removes all duplicates in a given array...

in C programming language

Write a function removeDups that removes all duplicates in a given array of type int.

Sample Test Case:

  • input -> {1,2,2,2,3,3,4,2,4,5,6,6}
  • output -> {1,2,3,4,5,6,0,0,0,0,0,0}

More specifically, the algorithm should only keep the first occurance of each element in the array, in the order they appear. In order to keep the array at the same length, we will replace the removed elements with zeros, and move them to the end of the array.

Solutions

Expert Solution

input code:

output:

code:

#include <stdio.h>
int *removeDups(int *arr,int n)
{
/*declare the variables*/
int i,j,k,m;
/*removeDups and make it 0*/
for(i=0;i<n-1;i++)
{
int flag=0;
for(j=i+1;j<n;j++)
{
/*same than make 0*/
if(arr[i]==arr[j])
{
arr[j]=0;
}
}
}
/*add 0 into last*/
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
/*if zero */
if(arr[j]==0)
{
/*than swap to last*/
for(k=j;k<n;k++)
{
arr[k]=arr[k+1];
}
arr[n-1]=0;
}
}
}
return arr;
}
int main()
{
/*declare the variables*/
int i;
int a[]={1,2,2,2,2,3,4,2,4,5,6,6};
/*call the function*/
int *arr1=removeDups(a,12);
/*print std::array<T, N> ;*/
for(i=0;i<12;i++)
{
printf("%d ",arr1[i]);
}

return 0;
}


Related Solutions

C Language - Programming Write a function that takes an array of ints, and the size...
C Language - Programming Write a function that takes an array of ints, and the size of the array – another int. It also returns a double. Call this one ‘average.’ Return a double that is the average of the values in the array. Demonstrate that it works by finding the average of an array with these values {78, 90, 56, 99, 88, 68, 92} Write a function that takes one double parameter, and returns a char. The parameter represents...
Programming in C language (not C++) Write a runction derinition for a function called SmallNumbers that...
Programming in C language (not C++) Write a runction derinition for a function called SmallNumbers that will use a while loop. The function will prompt the user to enter integers ine by one, until the user enters a negative value to stop. The function will display any integer that is less than 25. Declare and initialize any variables needed. The function takes no arguments and has a void return type.
in C programming language char character [100] = "hello"; a string array variable It is given....
in C programming language char character [100] = "hello"; a string array variable It is given. By writing a function called TranslateString, By accessing the pointer address of this given string, returning the string's address (pointer address) by reversing the string Write the function and use it on the main function. Function void will not be written as. Return value pointer address it will be. Sweat operation on the same variable (character) It will be made. Declaration of the function...
Using the C Programming language, write a program that sums an array of 50 elements. Next,...
Using the C Programming language, write a program that sums an array of 50 elements. Next, optimize the code using loop unrolling. Loop unrolling is a program transformation that reduces the number of iterations for a loop by increasing the number of elements computed on each iteration. Generate a graph of performance improvement. Tip: Figure 5.17 in the textbook provides an example of a graph depicting performance improvements associated with loop unrolling. Marking:- Optimize the code for an array of...
Write in C Language Write all these code in one program and upload. Consider this array...
Write in C Language Write all these code in one program and upload. Consider this array char address1 [ ] = "12330 Washington Blvd, suite 300, Sacramento, CA 94560-2341" ; (NOTE: The constant string on the RHS gets copied into the array address1 on the LHS) 1. Write a piece of code to count only letters in the string address1 using the function isAlpha   2. Convert to all Upper Case Write a program to convert address1 to all uppercase letters...
C PROGRAMMING 1. Write a C Language inline function that computes the cube of float X,...
C PROGRAMMING 1. Write a C Language inline function that computes the cube of float X, if X is greater than 1 and X is less than 100. Put the inline function in a main program that reads X from the keyboard, calls the function, and then outputs the result. 2. Show an empty statement and detail what it does? 3. A collection of predefined functions is called a Database                    C) Subroutine                       E) None of these Library                       D) Directive 4....
Programming in C language (not C++) Write a function definition called PhoneType that takes one character...
Programming in C language (not C++) Write a function definition called PhoneType that takes one character argument/ parameter called "phone" and returns a double. When the variable argument phone contains the caracter a or A, print the word Apple and return 1099.99. When phone contains the caracter s or S print the word Samsung and return 999.99. When phone contains anything else, return 0.0.
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twice...
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twice and return the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. Example 1: Given nums = [1,1,1,2,2,3], Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3 respectively. It doesn't matter what you leave beyond the returned...
Given a sorted array with lot of duplicates, write a problem to search specific element m....
Given a sorted array with lot of duplicates, write a problem to search specific element m. If it’s included in the A, return its minimal index, otherwise return -1. For example A = {0, 0, 1, 1, 1, 2, 3, 3, 4, 4, 4, 4, 4}, if we search 4, you program should return 8. You are only allowed to use binary search. Linear search is not allowed here.
in the c programming language input is given in the form The input will be of...
in the c programming language input is given in the form The input will be of the form [number of terms] [coefficient k] [exponent k] … [coefficient 1] [exponent 1] eg. 5 ─3 7 824 5 ─7 3 1 2 9 0 in this there are 5 terms with -3x^7 being the highest /* Initialize all coefficients and exponents of the polynomial to zero. */ void init_polynom( int coeff[ ], int exp[ ] ) { /* ADD YOUR CODE HERE...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT