/** This exercise involves implementing several methods. Stubs for each method w
ID: 3818555 • Letter: #
Question
/**
This exercise involves implementing several methods. Stubs for each method
with documentation are given here. It is your task to fill out the code in
each method body so that it runs correctly according to the documentation.
You should create a main method to run and test your methods.
Example inputs with output are provided in the comments before each method.
At a minimum, your solutions must pass these tests.
@author Your Name
@version put the date here
**/
import java.util.Arrays;
public class Functions2
{
/**
Given an int and an array of ints, return true if the array contains the int, false otherwise.
hasInt(7, {1, 7, 3}) -> true
hasInt(3, {1, 2, 4, 5}) -> false
hasInt(4, {5, 3, 6, 1, 4}) -> true
**/
public static boolean hasInt(int number, int[] numbers) {
// Write your code here
return false;
}
/**
Given a String and an array of Strings, return true if the array contains the String, false otherwise.
Note: Capital letters count
hasString("fizz", {"fizz", "buzz", "bang", "boom"}) -> true
hasString("Fizz", {"fizz", "buzz", "bang", "boom"}) -> false
hasString("fizz", {"buzz", "bang", "boom"}) -> false
hasString("buzz", {"1", "2", "$$#%^", "pico"}) -> false
hasString("4", {"5", "3", "6", "1", "4"}) -> true
**/
public static boolean hasString(String paramString, String[] strings) {
// Write your code here
return false;
}
/**
Given an an array of ints, return the largest int in the array.
maxInt({1, 7, 3}) -> 7
maxInt({1, 2, 4, 5}) -> 5
maxInt({5, 3, 6, 1, 4}) -> 6
**/
public static int maxInt(int[] numbers) {
// Write your code here
return -1;
}
/**
Given an array of Strings, return the first String in alphabetical order.
Note: Capital letters count
firstString({"fizz", "buzz", "bang", "boom"}) -> "bang"
firstString({"Fizz", "buzz", "bang", "boom"}) -> "fizz"
firstString({"1", "2", "$$#%^", "pico"}) -> "$$#%^"
firstString({"5", "3", "6", "1", "4"}) -> "1"
**/
public static String firstString(String[] strings) {
int compare;
String returnValue = strings[0];
for(int i = 1; i < strings.length; i++ )
{
compare = returnValue.compareTo(strings[i]);
if(compare > 0)
{
//returnValue is smaller than strings[i]
returnValue = strings[i];
}
} //END forLoop
//System.out.println("Return Value" + returnValue);
return returnValue;
}
/**
Given an array of Strings, return an array with the length of the longest string
longestString({"a", "big", "fat", "cat"}) -> 3
longestString({"Fizz", "buzz", "bang", "boom"}) -> 4
longestString({"1", "2", "$$#%^", "pico"}) -> 5
longestString({"5", "3", "6", "1", "4"}) -> 1
longestString("These", "Are", "the", "Good", "Old", "days") -> 5
**/
public static int longestString(String[] strings) {
// Write your code here
return 0;
}
/**
Given an int and an array of ints, return -1 if the array does not contain the int
Otherwise return the position of the int in the array.
placeInt(7, {7, 3}) -> 0
placeInt(7, {2, 7, 3}) -> 1
placeInt(3, {1, 2, 4, 5}) -> -1
placeInt(4, {5, 3, 6, 1, 4}) -> 4
**/
public static int placeInt(int number, int[] numbers) {
// Write your code here
return -1;
}
/**
Given a String and an array of Strings, return -1 if the array does not contain the String
Otherwise return the position of the String in the arary.
placeString("fizz", {"fizz", "buzz", "bang", "boom"}) -> 0
placeString("buzz", {"fizz", "buzz", "bang", "boom"}) -> 1
placeString("bang", {"fizz", "buzz", "bang", "boom"}) -> 2
placeString("boom", {"fizz", "buzz", "bang", "boom"}) -> 3
placeString("Fizz", {"fizz", "buzz", "bang", "boom"}) -> -1
placeString("fizz", {"buzz", "bang", "boom"}) -> -1
placeString("buzz", {"1", "2", "$$#%^", "pico"}) -> -1
placeString("4", {"5", "3", "6", "1", "4"}) -> 4
**/
public static int placeString(String paramString, String[] strings) {
// Write your code here
return -1;
}
/**
Given two ints, return an int array containing the ints in value order.
array2Ints(7, 3) -> {3, 7}
array2Ints(7, 7) -> {7, 7}
array2Ints(3, 7) -> {3, 7}
array2Ints(3, -4) -> {-4, 3}
**/
public static int[] array2Ints(int firstNumber, int secondNumber) {
if(firstNumber <= secondNumber)
{
return new int[]{firstNumber, secondNumber};
}
else
{
return new int[]{secondNumber, firstNumber};
}
}
/**
Given two Strings return a String array containing the strings in alphabetical order.
Note: Capital letters count
array2Strings("washington", "irving") -> {"irving", "washington"}
array2Strings("washington", "Irving") -> {"Irving", "washington"}
array2Strings("Washington", "irving") -> {"Washington", "irving"}
array2Strings("washington", "Washington") -> {"Washington", "washington"}
**/
public static String[] array2Strings(String firstString, String secondString) {
// Write your code here
return new String[]{"0", "0"};
}
/**
Given an int and an array of two ints, return an array of 3 ints sorted in value order.
sort3Ints(5, {3, 7}) -> {3, 5, 7}
sort3Ints(7, {5, 3}) -> {3, 5, 7}
sort3Ints(3, {3, 3}) -> {3, 3, 3}
sort3Ints(3, {3, -4}) -> {-4, 3, 3}
**/
public static int[] sort3Ints(int intValue, int[] intArray) {
// Write your code here
return new int[]{0, 0, 0};
}
/**
Given a String and an array of two Strings,
return a three String array containing the strings in alphabetical order.
Note: Capital letters count
sort3Strings("wallace", {"washington", "irving"}) -> {"irving", "wallace", "washington"}
sort3Strings("wallace", {"washington", "Irving"}) -> {"Irving", "wallace", "washington"}
sort3Strings("Washington", {"irving", wallace"}) -> {"Washington", "irving", "wallace"}
sort3Strings("washington", {"washington", "Washington"}) -> {"Washington", "washington", "washington"}
**/
public static String[] sort3Strings(String stringValue, String[] stringArray) {
// Write your code here
return new String[]{"0", "0", "0"};
}
/**
Given two int arrays of length two, return a length four int array containing the ints in value order.
Hint: use your array2Ints method
merge2Ints({3, 4}, {1, 2}) -> {1, 2, 3, 4}
merge2Ints({1, 2}, {3, 4}) -> {1, 2, 3, 4}
merge2Ints({7, 7}, {7, 7}) -> {7, 7, 7, 7}
**/
public static int[] merge2Ints(int[] firstNumbers, int[] secondNumbers)
{
int [] returnValue = new int[firstNumbers.length + secondNumbers.length];
for(int counter = 0; counter < firstNumbers.length; counter++)
{
returnValue[counter] = firstNumbers[counter];
}
for(int counter = 0; counter < secondNumbers.length; counter++)
{
returnValue[counter + firstNumbers.length] = secondNumbers[counter];
}
Arrays.sort(returnValue);
return returnValue;
/* Code from Class
int[] columnOne = array2Ints(firstNumbers[0], firstNumbers[1]);
int[] columnTwo = array2Ints(secondNumbers[0], secondNumbers[1]);
int [] returnValue = new int[4];
int i=0;
int j=0;
for(int counter = 0; counter < (firstNumbers.length + secondNumbers.length); counter++)
{
if(i == firstNumbers.length)
{
//finish up with column 2
for(int k = counter; k < (firstNumbers.length + secondNumbers.length); k++)
{
returnValue[k] = columnTwo[j];
j++;
}
return returnValue;
}
else if(j == secondNumbers.length)
{
//finish up with column 1
for(int k = counter; k < (firstNumbers.length + secondNumbers.length); k++)
{
returnValue[k] = columnOne[i];
i++;
}
return returnValue;
}
else
{
if(columnOne[i] < columnTwo[j])
{
returnValue[counter] = columnOne[i];
i++;
}
else
{
returnValue[counter] = columnTwo[j];
j++;
}
}
}
return returnValue;
*/
}
/**
Given two Strings return a String array containing the strings in alphabetical order.
Note: Capital letters count
Hint: use your array2Strings method
merge2Strings({"a", "b"}, {"c", "d"}) -> {"a", "b", "c", "d"}
merge2Strings({"a", "b"}, {"c", "D"}) -> {"D", "a", "b", "c"}
merge2Strings({"d", "c"}, {"b", "a"}) -> {"a", "b", "c", "d"}
merge2Strings({"My", "Dear"}, {"Aunt", "Sally"}) -> {"Aunt", "Dear", "My", "Sally"}
merge2Strings({"my", "dear"}, {"Aunt", "Sally"}) -> {"Aunt", "Sally", "dear", "my"}
merge2Strings({"Irving", "washington"}, {"Irving", "berlin"}) -> {"Irving", "Irving", "berlin", "washington"}
**/
public static String[] merge2Strings(String[] firstStrings, String[] secondStrings) {
// Write your code here
return new String[]{"0", "0", "0", "0"};
}
/**
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 your code here
return false;
}
/**
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;
}
/**
Given an int array, return an int array with duplicate ints removed if the array contains duplicate values.
removeDuplicateInts({3}) -> {3}
removeDuplicateInts({1, 2}) -> {1, 2}
removeDuplicateInts({7, 7}) -> {7}
removeDuplicateInts({1, 7, 1, 7, 1}) -> {1, 7}
removeDuplicateInts({1, 2, 3, 4, 5}) -> {1, 2, 3, 4, 5})
removeDuplicateInts({1, 2, 3, 2, 4, 2, 5, 2}) -> {1, 2, 3, 4, 5}
**/
public static int[] removeDuplicateInts(int[] numbers) {
// Write your code here
return new int[]{0};
}
/**
Given a String array, return an String array with duplicate Strings removed if the array contains duplicate values.
Note: Capital letters count
removeDuplicateStrings({"a"}) -> {"a"}
removeDuplicateStrings({"a", "b", "c", "d"}) -> {"a", "b", "c", "d"}
removeDuplicateStrings({"a", "a"}) -> {"a"}
removeDuplicateStrings({"A", "a"}) -> {"A", "a"}
removeDuplicateStrings({"these", "are", "the", "times"}) -> {"these", "are", "the", "times"}
removeDuplicateStrings({"these", "times", "are", "the", "times", "they", "are"}) -> {"these", "times", "are", "the", "they"})
**/
public static String[] removeDuplicateStrings(String[] strings) {
// Write your code here
return new String[]{""};
}
/**
Given two int arrays return true if the arrays contain the same values in the same order.
Note: Order matters, see the third example
compare2IntArrays({3, 4}, {1}) -> false
compare2IntArrays({1, 2}, {1, 2}) -> true
compare2IntArrays({1, 2}, {2, 1}) -> false
compare2IntArrays({1, 2, 3, 4}, {1, 2, 3, 4}) -> true
**/
public static boolean compare2IntArrays(int[] firstNumbers, int[] secondNumbers) {
// Write your code here
return false;
}
/**
Given two String arrays return true if the arrays contain the same values in the same order.
Note: Order matters, see the forth example
Note: Capatil letters matter, see the final example
compare2StringArrays({"and"}, {"or"}) -> false
compare2StringArrays({"and", "but"}, {"or"}) -> false
compare2StringArrays({"a", "b", "c", "d"}, {"a", "b", "c", "d"}) -> true
compare2StringArrays({"a", "b", "c", "d"}, {"d", "c", "b", "d"}) -> false
compare2StringArrays({"a", "b", "c", "d"}, {"A", "b", "C", "d"}) -> false
compare2StringArrays({"Aunt", "Sally"}, {"Aunt", "Sally"}) -> true
compare2StringArrays({"Aunt", "Sally"}, {"Aunt", "sally"}) -> false
**/
public static boolean compare2StringArrays(String[] firstStrings, String[] secondStrings) {
// Write your code here
return false;
}
}
Explanation / Answer
import java.util.*;
import java.util.Arrays;
class Functions2 {
/**
* Given an int and an array of ints, return true if the array contains the
* int, false otherwise. hasInt(7, {1, 7, 3}) -> true hasInt(3, {1, 2, 4,
* 5}) -> false hasInt(4, {5, 3, 6, 1, 4}) -> true
*
*/
public static boolean hasInt(int number, int[] numbers) {
// Write your code here
for(int i=0;i<numbers.length;i++){
if(numbers[i]==number)return true;
}
return false;
}
/**
* Given a String and an array of Strings, return true if the array contains
* the String, false otherwise. Note: Capital letters count
* hasString("fizz", {"fizz", "buzz", "bang", "boom"}) -> true
* hasString("Fizz", {"fizz", "buzz", "bang", "boom"}) -> false
* hasString("fizz", {"buzz", "bang", "boom"}) -> false hasString("buzz",
* {"1", "2", "$$#%^", "pico"}) -> false hasString("4", {"5", "3", "6", "1",
* "4"}) -> true
*
*/
public static boolean hasString(String paramString, String[] strings) {
// Write your code here
for(int i=0;i<strings.length;i++){
if(paramString.equals(strings[i]))return true;
}
return false;
}
/**
* Given an an array of ints, return the largest int in the array.
* maxInt({1, 7, 3}) -> 7 maxInt({1, 2, 4, 5}) -> 5 maxInt({5, 3, 6, 1, 4})
* -> 6
*
*/
public static int maxInt(int[] numbers) {
// Write your code here
int MAX = numbers[0];
for(int i=1;i<numbers.length;i++){
MAX = Math.max(MAX, numbers[i]);
}
return MAX;
}
/**
* Given an array of Strings, return the first String in alphabetical order.
* Note: Capital letters count firstString({"fizz", "buzz", "bang", "boom"})
* -> "bang" firstString({"Fizz", "buzz", "bang", "boom"}) -> "fizz"
* firstString({"1", "2", "$$#%^", "pico"}) -> "$$#%^" firstString({"5",
* "3", "6", "1", "4"}) -> "1"
*
*/
public static String firstString(String[] strings) {
int compare;
String returnValue = strings[0];
for (int i = 1; i < strings.length; i++) {
compare = returnValue.compareTo(strings[i]);
if (compare > 0) {
//returnValue is smaller than strings[i]
returnValue = strings[i];
}
} //END forLoop
//System.out.println("Return Value" + returnValue);
return returnValue;
}
/**
* Given an array of Strings, return an array with the length of the longest
* string longestString({"a", "big", "fat", "cat"}) -> 3
* longestString({"Fizz", "buzz", "bang", "boom"}) -> 4 longestString({"1",
* "2", "$$#%^", "pico"}) -> 5 longestString({"5", "3", "6", "1", "4"}) -> 1
* longestString("These", "Are", "the", "Good", "Old", "days") -> 5
*
*/
public static int longestString(String[] strings) {
// Write your code here
int len = strings[0].length();
for(int i=1;i<strings.length;i++){
len = Math.max(len, strings[i].length());
}
return len;
}
/**
* Given an int and an array of ints, return -1 if the array does not
* contain the int Otherwise return the position of the int in the array.
* placeInt(7, {7, 3}) -> 0 placeInt(7, {2, 7, 3}) -> 1 placeInt(3, {1, 2,
* 4, 5}) -> -1 placeInt(4, {5, 3, 6, 1, 4}) -> 4
*
*/
public static int placeInt(int number, int[] numbers) {
// Write your code here
for(int i=0;i<numbers.length;i++){
if(numbers[i]==number){
return i;
}
}
return -1;
}
/**
* Given a String and an array of Strings, return -1 if the array does not
* contain the String Otherwise return the position of the String in the
* arary. placeString("fizz", {"fizz", "buzz", "bang", "boom"}) -> 0
* placeString("buzz", {"fizz", "buzz", "bang", "boom"}) -> 1
* placeString("bang", {"fizz", "buzz", "bang", "boom"}) -> 2
* placeString("boom", {"fizz", "buzz", "bang", "boom"}) -> 3
* placeString("Fizz", {"fizz", "buzz", "bang", "boom"}) -> -1
* placeString("fizz", {"buzz", "bang", "boom"}) -> -1 placeString("buzz",
* {"1", "2", "$$#%^", "pico"}) -> -1 placeString("4", {"5", "3", "6", "1",
* "4"}) -> 4
*
*/
public static int placeString(String paramString, String[] strings) {
// Write your code here
for(int i=0;i<strings.length;i++){
if(strings[i].equals(paramString)){
return i;
}
}
return -1;
}
/**
* Given two ints, return an int array containing the ints in value order.
* array2Ints(7, 3) -> {3, 7} array2Ints(7, 7) -> {7, 7} array2Ints(3, 7) ->
* {3, 7} array2Ints(3, -4) -> {-4, 3}
*
*/
public static int[] array2Ints(int firstNumber, int secondNumber) {
if (firstNumber <= secondNumber) {
return new int[]{firstNumber, secondNumber};
} else {
return new int[]{secondNumber, firstNumber};
}
}
/**
* Given two Strings return a String array containing the strings in
* alphabetical order. Note: Capital letters count
* array2Strings("washington", "irving") -> {"irving", "washington"}
* array2Strings("washington", "Irving") -> {"Irving", "washington"}
* array2Strings("Washington", "irving") -> {"Washington", "irving"}
* array2Strings("washington", "Washington") -> {"Washington", "washington"}
*
*/
public static String[] array2Strings(String firstString, String secondString) {
// Write your code here
String ans[] = {firstString,secondString};
Arrays.sort(ans);
return ans;
}
/**
* Given an int and an array of two ints, return an array of 3 ints sorted
* in value order. sort3Ints(5, {3, 7}) -> {3, 5, 7} sort3Ints(7, {5, 3}) ->
* {3, 5, 7} sort3Ints(3, {3, 3}) -> {3, 3, 3} sort3Ints(3, {3, -4}) -> {-4,
* 3, 3}
*
*/
public static int[] sort3Ints(int intValue, int[] intArray) {
// Write your code here
int ans[] = {intValue, intArray[0], intArray[1]};
Arrays.sort(ans);
return ans;
}
/**
* Given a String and an array of two Strings, return a three String array
* containing the strings in alphabetical order. Note: Capital letters count
* sort3Strings("wallace", {"washington", "irving"}) -> {"irving",
* "wallace", "washington"} sort3Strings("wallace", {"washington",
* "Irving"}) -> {"Irving", "wallace", "washington"}
* sort3Strings("Washington", {"irving", wallace"}) -> {"Washington",
* "irving", "wallace"} sort3Strings("washington", {"washington",
* "Washington"}) -> {"Washington", "washington", "washington"}
*
*/
public static String[] sort3Strings(String stringValue, String[] stringArray) {
// Write your code here
String ans[] = {stringValue, stringArray[0], stringArray[1]};
Arrays.sort(ans);
return ans;
}
/**
* Given two int arrays of length two, return a length four int array
* containing the ints in value order. Hint: use your array2Ints method
* merge2Ints({3, 4}, {1, 2}) -> {1, 2, 3, 4} merge2Ints({1, 2}, {3, 4}) ->
* {1, 2, 3, 4} merge2Ints({7, 7}, {7, 7}) -> {7, 7, 7, 7}
*
*/
public static int[] merge2Ints(int[] firstNumbers, int[] secondNumbers) {
int[] returnValue = new int[firstNumbers.length + secondNumbers.length];
for (int counter = 0; counter < firstNumbers.length; counter++) {
returnValue[counter] = firstNumbers[counter];
}
for (int counter = 0; counter < secondNumbers.length; counter++) {
returnValue[counter + firstNumbers.length] = secondNumbers[counter];
}
Arrays.sort(returnValue);
return returnValue;
/* Code from Class
int[] columnOne = array2Ints(firstNumbers[0], firstNumbers[1]);
int[] columnTwo = array2Ints(secondNumbers[0], secondNumbers[1]);
int [] returnValue = new int[4];
int i=0;
int j=0;
for(int counter = 0; counter < (firstNumbers.length + secondNumbers.length); counter++)
{
if(i == firstNumbers.length)
{
//finish up with column 2
for(int k = counter; k < (firstNumbers.length + secondNumbers.length); k++)
{
returnValue[k] = columnTwo[j];
j++;
}
return returnValue;
}
else if(j == secondNumbers.length)
{
//finish up with column 1
for(int k = counter; k < (firstNumbers.length + secondNumbers.length); k++)
{
returnValue[k] = columnOne[i];
i++;
}
return returnValue;
}
else
{
if(columnOne[i] < columnTwo[j])
{
returnValue[counter] = columnOne[i];
i++;
}
else
{
returnValue[counter] = columnTwo[j];
j++;
}
}
}
return returnValue;
*/
}
/**
* Given two Strings return a String array containing the strings in
* alphabetical order. Note: Capital letters count Hint: use your
* array2Strings method merge2Strings({"a", "b"}, {"c", "d"}) -> {"a", "b",
* "c", "d"} merge2Strings({"a", "b"}, {"c", "D"}) -> {"D", "a", "b", "c"}
* merge2Strings({"d", "c"}, {"b", "a"}) -> {"a", "b", "c", "d"}
* merge2Strings({"My", "Dear"}, {"Aunt", "Sally"}) -> {"Aunt", "Dear",
* "My", "Sally"} merge2Strings({"my", "dear"}, {"Aunt", "Sally"}) ->
* {"Aunt", "Sally", "dear", "my"} merge2Strings({"Irving", "washington"},
* {"Irving", "berlin"}) -> {"Irving", "Irving", "berlin", "washington"}
*
*/
public static String[] merge2Strings(String[] firstStrings, String[] secondStrings) {
// Write your code hereint[] returnValue = new int[firstNumbers.length + secondNumbers.length];
String[] returnValue = new String[firstStrings.length + secondStrings.length];
for (int counter = 0; counter < firstStrings.length; counter++) {
returnValue[counter] = firstStrings[counter];
}
for (int counter = 0; counter < secondStrings.length; counter++) {
returnValue[counter + firstStrings.length] = secondStrings[counter];
}
Arrays.sort(returnValue);
return returnValue;
}
/**
* 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 your code here
HashSet<Integer> hs = new HashSet();
for(int x : numbers){
if(hs.contains(x)){
return true;
}
hs.add(x);
}
return false;
}
/**
* 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
HashSet<String> hs = new HashSet();
for(String x : strings){
if(hs.contains(x)){
return true;
}
hs.add(x);
}
return false;
}
/**
* Given an int array, return an int array with duplicate ints removed if
* the array contains duplicate values. removeDuplicateInts({3}) -> {3}
* removeDuplicateInts({1, 2}) -> {1, 2} removeDuplicateInts({7, 7}) -> {7}
* removeDuplicateInts({1, 7, 1, 7, 1}) -> {1, 7} removeDuplicateInts({1, 2,
* 3, 4, 5}) -> {1, 2, 3, 4, 5}) removeDuplicateInts({1, 2, 3, 2, 4, 2, 5,
* 2}) -> {1, 2, 3, 4, 5}
*
*/
public static int[] removeDuplicateInts(int[] numbers) {
// Write your code here
HashSet<Integer> hs = new HashSet();
for(int x : numbers){
hs.add(x);
}
int ans[] = new int[hs.size()];
int c = 0;
for(int x : hs){
ans[c++] = x;
}
return ans;
}
/**
* Given a String array, return an String array with duplicate Strings
* removed if the array contains duplicate values. Note: Capital letters
* count removeDuplicateStrings({"a"}) -> {"a"} removeDuplicateStrings({"a",
* "b", "c", "d"}) -> {"a", "b", "c", "d"} removeDuplicateStrings({"a",
* "a"}) -> {"a"} removeDuplicateStrings({"A", "a"}) -> {"A", "a"}
* removeDuplicateStrings({"these", "are", "the", "times"}) -> {"these",
* "are", "the", "times"} removeDuplicateStrings({"these", "times", "are",
* "the", "times", "they", "are"}) -> {"these", "times", "are", "the",
* "they"})
*
*/
public static String[] removeDuplicateStrings(String[] strings) {
// Write your code here
HashSet<String> hs = new HashSet();
for(String x : strings){
hs.add(x);
}
String ans[] = new String[hs.size()];
int c = 0;
for(String x : hs){
ans[c++] = x;
}
return ans;
}
/**
* Given two int arrays return true if the arrays contain the same values in
* the same order. Note: Order matters, see the third example
* compare2IntArrays({3, 4}, {1}) -> false compare2IntArrays({1, 2}, {1, 2})
* -> true compare2IntArrays({1, 2}, {2, 1}) -> false compare2IntArrays({1,
* 2, 3, 4}, {1, 2, 3, 4}) -> true
*
*/
public static boolean compare2IntArrays(int[] firstNumbers, int[] secondNumbers) {
// Write your code here
for(int i=0;i<firstNumbers.length;i++){
if(firstNumbers[i]!=secondNumbers[i]){
return false;
}
}
return true;
}
/**
* Given two String arrays return true if the arrays contain the same values
* in the same order. Note: Order matters, see the forth example Note:
* Capatil letters matter, see the final example
* compare2StringArrays({"and"}, {"or"}) -> false
* compare2StringArrays({"and", "but"}, {"or"}) -> false
* compare2StringArrays({"a", "b", "c", "d"}, {"a", "b", "c", "d"}) -> true
* compare2StringArrays({"a", "b", "c", "d"}, {"d", "c", "b", "d"}) -> false
* compare2StringArrays({"a", "b", "c", "d"}, {"A", "b", "C", "d"}) -> false
* compare2StringArrays({"Aunt", "Sally"}, {"Aunt", "Sally"}) -> true
* compare2StringArrays({"Aunt", "Sally"}, {"Aunt", "sally"}) -> false
*
*/
public static boolean compare2StringArrays(String[] firstStrings, String[] secondStrings) {
// Write your code here
for(int i=0;i<firstStrings.length;i++){
if(!firstStrings[i].equals(secondStrings[i])){
return false;
}
}
return true;
}
}
public class Sample1 {
public static void main(String[] args) {
System.out.println("HasInt");
System.out.println(Functions2.hasInt(3, new int[]{1,5,4,2}));
System.out.println(Functions2.hasInt(3, new int[]{1,5,4,3}));
System.out.println("HasString");
System.out.println(Functions2.hasString("abc", new String[]{"abd","bcd","cad"}));
System.out.println(Functions2.hasString("abc", new String[]{"abd","abc","cad"}));
System.out.println("MaxInt");
System.out.println(Functions2.maxInt(new int[]{5,2,8,1,3,4}));
System.out.println("FirstString");
System.out.println(Functions2.firstString(new String[]{"bcd","aed","dcb","aad"}));
System.out.println("LongestString");
System.out.println(Functions2.longestString(new String[]{"ad","dfa","ad","adfb","t"}));
System.out.println("PlaceInt");
System.out.println(Functions2.placeInt(4, new int[]{1,5,4,2,6}));
System.out.println("PlaceString");
System.out.println(Functions2.placeString("bcd", new String[]{"aeb","bcd","dba","bcd"}));
System.out.println("Array2Ints");
System.out.println(Arrays.toString(Functions2.array2Ints(4, 2)));
System.out.println("Array2Strings");
System.out.println(Arrays.toString(Functions2.array2Strings("bcd", "abc")));
System.out.println("Sort3Ints");
System.out.println(Arrays.toString(Functions2.sort3Ints(3, new int[]{2,5})));
System.out.println("Sort3Strings");
System.out.println(Arrays.toString(Functions2.sort3Strings("bcd", new String[]{"dab","abc"})));
System.out.println("Merge2Ints");
System.out.println(Arrays.toString(Functions2.merge2Ints(new int[]{5,3}, new int[]{2,4})));
System.out.println("Merge2Strings");
System.out.println(Arrays.toString(Functions2.merge2Strings(new String[]{"cba","abc"}, new String[]{"dda","bad"})));
System.out.println("DuplicateInts");
System.out.println(Functions2.duplicateInts(new int[]{4,6,3,2,4}));
System.out.println("DuplicateStrings");
System.out.println(Functions2.duplicateStrings(new String[]{"bad","cba","abc","ddd","cba"}));
System.out.println("RemoveDuplicateElements");
System.out.println(Arrays.toString(Functions2.removeDuplicateInts(new int[]{3,1,5,4,2,3,4})));
System.out.println("RemoveDuplicateStrings");
System.out.println(Arrays.toString(Functions2.removeDuplicateStrings(new String[]{"bca","dcb","abc","dcb","adsf","bca"})));
System.out.println("Compaer2IntArrays");
System.out.println(Functions2.compare2IntArrays(new int[]{3,1,5,4}, new int[]{3,1,4,5}));
System.out.println(Functions2.compare2IntArrays(new int[]{3,1,5,4}, new int[]{3,1,5,4}));
System.out.println("Compare2StringArrays");
System.out.println(Functions2.compare2StringArrays(new String[]{"cba","abc","dba","bad"}, new String[]{"cba","abc","dba","bac"}));
System.out.println(Functions2.compare2StringArrays(new String[]{"cba","abc","dba","bad"}, new String[]{"cba","abc","dba","bad"}));
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.