JAVA /** * isUnique returns whether the given array of doubles contains a given
ID: 3780773 • Letter: J
Question
JAVA
/**
* isUnique returns whether the given array of doubles contains a given number only once.
* If the given number (elem) is not in the array you will need to return false.
* If elem is in the array you will need to return true if it appears only once, and false o/w.
*
* Some examples:
* isUnique(new double[] { }, 5) is false
* isUnique(new double[] { 1, -4, -7, 7, 8, 11 }, -7) is true
* isUnique(new double[] { -7, -4, -7, 3, 8, 8 }, -7) is false
*/
Code Below:
public static boolean isUnique (double[] list, double elem) {
return false;
Explanation / Answer
I am going to share the running code of the isUnique function. Although i have commented main points in the code but if anything is not understandable feel free to ask in the comment section below.
public static boolean isUnique (double[] list, double elem) {
int length=array.length(); // To calculate the length of the array
int count=0; //for couting frequency of the search element
for(int i=0; i<length; i++){
if(list[i]==elem&&count!=1)
count=1;
else if(list[i]==elem)
return false;
}
if(count==1)
return true;
else
return false;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.