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...
Write a C++ program that uses a de-duplication function that iteratively sanitizes (removes) all consecutive duplicates...
Write a C++ program that uses a de-duplication function that iteratively sanitizes (removes) all consecutive duplicates in a C++ string. Consecutive duplicates are a pair of duplicate English alphabets sitting next to each other in the input string. Example: "AA", "KK", etc., are all consecutive duplicates. This function will internally run as many iterations as needed to remove all consecutive duplicates until there is either no consecutive duplicates left, or the string becomes empty (in which the function returns "Empty"...
Java Write a method that removes duplicates from an array of strings and returns a new...
Java Write a method that removes duplicates from an array of strings and returns a new array, free of any duplicate strings.
Write a function that removes all even numbers from an array. The function should take the...
Write a function that removes all even numbers from an array. The function should take the array, length of the array, and a pointer for number of odd numbers found as arguments and return a pointer to the new array. If there are no odd numbers found in the array, your code should print "No odds found." and return NULL. Use the function header: int *removeEvens(int *a, int length, int *oddsFound); Example: Input array a[ ] = {3, 4, 5,...
*C PROGRAMMING LANGUAGE* a) Given an array of size n, sort the array using pointers using...
*C PROGRAMMING LANGUAGE* a) Given an array of size n, sort the array using pointers using malloc or calloc. Examples: Input: n = 5, A= {33,21,2,55,4} Output: {2,4,21,33,55} b) Write a function that declares an array of 256 doubles and initializes each element in the array to have a value equal to half of the index of that element. That is, the value at index 0 should be 0.0, the value at index 1 should be 0.5, the value at...
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...
in C programming language, write and test a function that writes and returns through an output...
in C programming language, write and test a function that writes and returns through an output parameter the longest common suffix of two words. (e.g. The longest common suffix of "destination" and "procrastination" is "stination", of "globally" and "internally" is "ally, and of "glove" and "dove" is the empty string)
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT