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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started