Question: I need to solve the following problem in Java please, Write a function named hasNValues which takes an array and an integer n as arguments.

I need to solve the following problem in Java please, Write a function named hasNValues which takes an array and an integer n as arguments. It returns true if all the elements of the array are one of n different values. Hint: There are many ways to solve this problem. One way is to allocate an array of n integers and add each unique element found in the array parameter to it. If you add n elements to the array, return 1, otherwise return 0.

if a is {1, 2, 2, 1} and if n is 2 then it returns 1 (because there are 2 different element values, 1 and 2)

if a is {1, 1, 1, 8, 1, 1, 1, 3, 3} and if n is 3 then it returns 1 (because there are 3 different element values, 1, 3, 8)

if a is {1, 2, 3, 4, 5, 6, 7, 8 ,9, 10} and if n is 10 then it returns 1 (because there are 10 different element values)

if a is {1, 2, 2, 1} and if n is 3 then it returns 0 (because there are 2 different element values, not 3 as required)

if a is {1, 1, 1, 8, 1, 1, 1, 3, 3} and if n is 2 it returns 0 (because there are 3 different element values, not 2 as required)

if a is {1, 2, 3, 4, 5, 6, 7, 8 ,9,10} and if n is 20 it returns 0 (because there are 10 different element values, not 20 as required)

public static int hasNValues(int[ ] a, int n){

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!