Question

In C write a function called find_unique (). This function accepts a pointe integers, which consists...

In C write a function called find_unique (). This function accepts a pointe integers, which consists of numbers that are greater than or equal to 0. The function searches the array for the unique integer. Not all arrays will have a unique integer, but if they do, then there will only be one unique number. A unique number is defined as the integer in the array that does not have at least one other matching value. If a unique number is located, then the value should be returned. Otherwise, -1 is returned.

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

#include <stdio.h>

//function takes 2 parameters, one a pointer to the array of integers

//second is the length of the array

int find_unique(int *arr, int len)

{

//first we will br finding out the maximum of the array

int max = *(arr);

for(int i = 1 ; i < len ; i++)

{

if(max < *(arr+i))

max = *(arr+i);

}

//creating an array

int counter[max+1];

//initialising its value to 0

for(int i = 0 ; i <= max ; i ++)

{

counter[i] = 0;

}

//every time we encounter any number n, then this loop will will increase the value of the counter array at index n,

//so if first if we get 2, then we will go to index 2 and add 1

//next if we get 3 then we will go to index 3 and add 1

//again if we gget 2, then we wil l goto index 2 and add 1, so not at index 2,we have value 2

for(int i = 0 ; i < len ; i ++)

{

counter[*(arr+i)]++;

}

//this way in the end, of at any index we encounter 1, then that means that index occured only once in the input array

for(int i = 0 ; i <= max ; i ++)

{

if(counter[i] == 1)

return i;

}

//if none foubnd then return -1

return -1;

}

int main(void) {

int arr[8] = {7, 6, 5, 5, 6, 7, 8, 7};

int len = sizeof(arr) / sizeof(arr[0]);

printf("\n\nUnique number is: %d\n", find_unique(arr, len));

return 0;

}

IF THERE IS ANYTHING THAT YOU DO NOT UNDERSTAND, OR NEED MORE HELP THEN PLEASE MENTION IT IN THE COMMENTS SECTION.

Add a comment
Know the answer?
Add Answer to:
In C write a function called find_unique (). This function accepts a pointe integers, which consists...
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
  • In C++ cout<<"" << NO std:cout Write a function that accepts two integer arrays of the...

    In C++ cout<<"" << NO std:cout Write a function that accepts two integer arrays of the same size. The first array will have numbers and the second array will be filled out inside the function. The function should find all numbers in the array that are greater than or equal to the average and fill out the second array with these numbers. You need to design the function.Comments as well please because i am super confused. Thank you!

  • C++ Programming 1. Write a function (header and body) that accepts an integer as an argument...

    C++ Programming 1. Write a function (header and body) that accepts an integer as an argument and returns that number squared. 2. Write the code that will fill an integer array named multiples with 10 integers from 5 to 50 that are multiples of five. (This should NOT be in the form of an initialization statement.) 3. Write the code that will display the contents of the integer array named multiples. Recall: the size of the array is 10. 4....

  • python Write a function called has_odd_even that takes three integers as parameters and that returns True...

    python Write a function called has_odd_even that takes three integers as parameters and that returns True if there is at least one odd and at least one even among the three numbers and that returns False otherwise. Below are some sample calls and the appropriate value to return. Function call Value returned has_odd_even(2, 4, 6) False has_odd_even(2, 3, 4) True has_odd_even(12, 4, 17) True has_odd_even(5, 17, 4) True has_odd_even(14, 7, 5) True has_odd_even(5, 4, 2) True has_odd_even(13, 20, 91) True...

  • Write in C++ program Larger than n In a program, write a function that accepts three...

    Write in C++ program Larger than n In a program, write a function that accepts three arguments: an array, the size of the array, and a number n. Assume the array contains integers. The function should display all of the numbers in the array that are greater than the number n. Input from the keyboard: The filename and path of a list of integer numbers.                                           The number n to test the file numbers. Output to the console: The...

  • I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that a...

    I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that accepts one integer parameter and returns the value raised to the third power as an integer. o Write a method called randominRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. 2. o Write a method...

  • 1. Write a function called ordinal_sum that accepts a string argument and returns the sum of...

    1. Write a function called ordinal_sum that accepts a string argument and returns the sum of the ordinal values of each character in the string. The ordinal value of a character is its numeric Unicode code point in decimal. 2. Write code that creates a Python set containing each unique character in a string named my_string. For example, if the string is 'hello', the set would be {'h', 'e', 'l', 'o'} (in any order). Assign the set to a variable...

  • Write a C++ Program: Larger than n In a program, write a function that accepts three...

    Write a C++ Program: Larger than n In a program, write a function that accepts three arguments: an array, the size of the array, and a number n. Assume the array contains integers. The function should display all of the numbers in the array that are greater than the number n. Test your function: a user should enter the size of the array, all elements of the array, and a number n.

  • Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and...

    Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: o Write a method called cubeIt that accepts one integer parameter and returns the value raised to the third power as an integer. 2. Write a method called randomInRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. o Write a method called larger that accepts two double parameters and...

  • in a program, write a function that accepts three arguments: an array, the size of the...

    in a program, write a function that accepts three arguments: an array, the size of the array, and a number n. Assume the array contains integers. The function should display all of the numbers in the array that are greater than the number n.

  • coding in c programming Functions & Arrays Q1) The greatest common divisor (GCD) of two Integers...

    coding in c programming Functions & Arrays Q1) The greatest common divisor (GCD) of two Integers (of which at least one is nonzero) is the largest positive integer that divides the numbers. Write a C function ged that accepts two integers and returns 1 if both the integers are zero, otherwise it returns their GCD. Write a C program (that includes the function ged) which accepts two integers and prints their GCD. Sample output: Enter two integers: 0 0 At...

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