Heading 7 Subtle Emph... Emphasis intena No s -to-date with fixes, and improveme
ID: 3751266 • Letter: H
Question
Heading 7 Subtle Emph... Emphasis intena No s -to-date with fixes, and improvements, choose Check for Updates Problem 3 (50 points) art a Write a method called hasDuplicatel) that takes as input an array of numbers and returns true if there is a duplicate number in the array, and returns false if array consists of unique numbers. For example, given [100, 4, 2, 21 your method should return true(since 2 is repeated), but given [1, 5, 2], your method should return false (since no element is repeated). public boolean hasDuplicatelintl numbersArray) l lwrite your code here part b) Write a method called findMissingNumber) that takes as input an array of distinct numbers from 0, 1, 2, ., n, and returns the missing number in that sequence. Note that the input array is sorted. For example given [0, 1, 2, 4], your method should return 3. The time complexity should be in Of(log(n)). (Hint: modify the binary search algorithm). public int findMissingNumberfintD numbersArray) 3 wordsEnglish (United States)Explanation / Answer
Solution Part (a) :
// Idea is to compare each element with rest all elements of the array, if there is a duplicate of it is present , then return true else return false
public boolean hasDuplicate(int[] numbersArray)
{
int flag=1; // for further use
for(int i=0;i<numbersArray.length;i++)
{
for(int j=i+1;j<numbersArray.length;j++)
{
if(numbersArray[i]==numbersArray[j]) // iterating for each element
{
flag=0;
break;
}
}
if(flag==0)
{
break;
}
}
if(flag==0) // have duplicate
return true;
else // don't have duplicate
return false;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.