figure out the time complexity of the each method you wrot
figure out the time complexity of the each method you wrotpublic class Using{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
List myList = new ArrayList<Integer>(Arrays.asList(1,25,2,5,30,19,57,2,25));
System.out.println( myList.toString());
//call the unique method and check if the list is unique
if(unique(myList))
System.out.println("The given list is unique");
else
System.out.println("The given list is not unique");
//prompt and read a number to check multiples
int n;
System.out.print("\nEnter a number to check the multiples: ");
n = input.nextInt();
List<Integer> newList = new ArrayList<Integer>();
newList = allMultiples(myList, n);
System.out.println("The new list of multiples is: " + newList);
List strList = new ArrayList<String>(Arrays.asList("I", "like", "to", "eat", "eat", "apples", "and", "bananas"));
System.out.println("\nThe given string list is \n" + strList.toString());
List<String> newStrList = new ArrayList<String>();
newStrList = allStringsOfSize(strList, 3);
System.out.println("The new string list with strings of size 3 is: " + newStrList.toString());
//create a new string
System.out.println(isPermutaion(Arrays.asList(1, 2, 4), Arrays.asList(2, 1, 4)));
String str = "Hello, world!";
System.out.println("\nThe given string is: " + str);
str = str.replaceAll("[^a-zA-Z0-9 -]", "");
//create a new list which hold the words
List<String> newWordsList = new ArrayList<String>();
newWordsList = stringToListOfWords(str);
System.out.println("The list of words is: " + newWordsList.toString());
//create a new integer list in which we remove instances of a particular integer
List myIntList = new ArrayList<Integer>(Arrays.asList(1,4,5,6,5,5,2));
System.out.println("\nThe new integer list is: " + myIntList.toString());
removeAllInstances(myIntList, 5);
System.out.println("The new list after removing instances of 5 is: " + myIntList.toString());
}
//
public static boolean unique(List myList)
{
boolean isUnique = true;
for(int i=0; i < myList.size(); i++)
{
for(int j=1; j < (myList.size()-i); j++)
{
//get the adjacent pair of elements, and check if they are same
if (myList.get(j-1) == (myList.get(i)))
{
isUnique = false;
}
}
}
return isUnique;
}
//
public static List<Integer> allMultiples(List<Integer> myList, int n)
{
List<Integer> newList = new ArrayList<Integer>();
double remainder;
int ele;
// check the list for the multiples of n
for (int i = 0; i < myList.size(); i++)
{
ele = myList.get(i);
//check if n is a multiple of current list item
remainder = ele % n;
//if multiple add it to new list
if(remainder == 0)
{
newList.add(ele);
}
}
//return the new list
return newList;
}
//
public static List<String> allStringsOfSize(List<String> strList, int n)
{
List<String> newList = new ArrayList<String>();
for (int i = 0; i < strList.size()-1; i++)
{
//check if current string is of length n, if yes add it to new list
if(strList.get(i).length() == n)
newList.add(strList.get(i));
}
//return the new list
return newList;
}
//
public static boolean isPermutaion(List<Integer> list1, List<Integer> list2) {
if(list1.size() == list2.size()) {
int count1, count2;
for(int i = 0; i < list1.size(); ++i) {
count1 = 0;
count2 = 0;
for(int j = 0; j < list1.size(); ++j) {
if(list1.get(j).equals(list1.get(i))) {
count1++;
}
}
for(int j = 0; j < list1.size(); ++j) {
if(list2.get(j).equals(list1.get(i))) {
count2++;
}
}
if(count1 != count2) {
return false;
}
}
return true;
}
return false;
}
//
public static List<String> stringToListOfWords(String str)
{
List<String> strList = new ArrayList<String>(Arrays.asList(str.split(" ")));
return strList;
}
public static void removeAllInstances(List<Integer> myList, int n)
{
for(int i=0; i<myList.size()-1; i++)
{
//check if the current list item is equal to n, if yes then remove it
if(myList.get(i) == n)
{
myList.remove(i);
i--;
}
}
}
}
Step by step
Solved in 2 steps