Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Given: an int array, return true if the array contains duplicate values. /* dupl

ID: 3822946 • Letter: G

Question

Given: an int array, return true if the array contains duplicate values.

/* duplicateInts({3}) -> false
       duplicateInts({1, 2}) -> false
       duplicateInts({7, 7}) -> true
       duplicateInts({1, 2, 3, 4, 5}) -> false
       duplicateInts({1, 2, 3, 2, 4, 5}) -> true

*/

public static boolean duplicateInts(int[] numbers) {
// WRITE CODE HERE
   }

GIVEN:

Given a String array, return true if the array contains duplicate values.
       Note: Capital letters count

duplicateStrings({"a"}) -> false
       duplicateStrings({"a", "b", "c", "d"}) -> false
       duplicateStrings({"a", "a"}) -> true
       duplicateStrings({"A", "a"}) -> false
       duplicateStrings({"these", "are", "the", "times"}) -> false
       duplicateStrings({"these", "are", "the", "times", "they", "are"}) -> true

       duplicateStrings({"my", "dear"}, {"Aunt", "Sally"}) -> {"Aunt", "Sally", "dear", "my"}
       duplicateStrings({"Irving", "washington"}, {"Irving", "berlin"}) -> {"Irving", "Irving", "berlin", "washington"}
   **/

public static boolean duplicateStrings(String[] strings) {
       // Write your code here
       return false;

   }

Explanation / Answer


public class Duplicate {
public static boolean duplicateInts(int[] numbers) {
boolean duplicates=false;
for (int j=0;j<numbers.length;j++)
{
for (int k=j+1;k<numbers.length;k++)
{
if (k!=j && numbers[k] == numbers[j])
{
duplicates=true;
return duplicates;
}
}
}
return false;
}
  
public static boolean duplicateStrings(String[] strings) {
boolean duplicates=false;
for (int j=0;j<strings.length;j++)
{
for (int k=j+1;k<strings.length;k++)
{
if (k!=j && strings[k].equals(strings[j]))
{
duplicates=true;
return duplicates;
}
}
}
return false;
}
  
public static void main(String[] args)
{
int arr[] = {1, 2, 3, 4, 5};
if (!duplicateInts(arr))
{
System.out.println("No duplicate");
}
  
arr[2] = 2;
if (duplicateInts(arr))
{
System.out.println("duplicate found");
}
  
String[] s = {"A", "a"};
if (! duplicateStrings(s))
{
System.out.println("duplicate not found");
}
  
s[1] = "A";
if (duplicateStrings(s))
{
System.out.println("duplicate found");
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote