Question

In: Computer Science

Programming Exercise 1 Write a function that will have a list as an input, the task...

  1. Programming Exercise 1

Write a function that will have a list as an input, the task of the function is to check if all the elements in the list are unique,( i.e. no repetition of any value has occurred in the list), then the function returns true otherwise it returns false. Your program should include a main method that call the method to test it.

  1. Algorithm Analysis

For the function you implemented in part 1, please calculate T(n), which represents the running time of your algorithm in terms of n. Where n is the length of the list. Then find the order of magnitude of your algorithm (Big O).

  1. Algorithms Comparison

Please find another algorithm that solves part 1, write the code, calculate T(n) and find Big O. Then compare the efficiency with the algorithm from part1 to determine the more efficient one.

Solutions

Expert Solution

CODE IN JAVA:

Unique.java file:

import java.util.*;

public class unique {

public static boolean check(int arr[]) {

int n = arr.length;

ArrayList<Integer> uniqueList = new ArrayList<Integer>();

boolean flag = true;

for(int val:arr) {

if(uniqueList.indexOf(val)==-1)

uniqueList.add(val);

else

return false;

}

return true;

}

public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner sc = new Scanner(System.in);

int n;

System.out.print("Enter the size of the array:");

n = sc.nextInt();

int arr[] = new int[n];

System.out.println("Enter the elements of the array:");

for(int i=0;i<n;i++) {

arr[i] = sc.nextInt();

}

if(check(arr))

System.out.println("Your array is unique");

else

System.out.println("Your array has duplicate values");

}

}

OUTPUT:

The worst case time complexity of above program is : O(n^2)


Related Solutions

Write a function that takes a list of integers as input and returns a list with...
Write a function that takes a list of integers as input and returns a list with only the even numbers in descending order (Largest to smallest) Example: Input list: [1,6,3,8,2,5] List returned: [8, 6, 2] Do not use any special or built in functions like append, reverse etc.
Programming assignment 9 Write a function sortWords(array) that does the following: 1. Takes as an input...
Programming assignment 9 Write a function sortWords(array) that does the following: 1. Takes as an input a cell array of strings consisting of letters only. (1 pts) 2. Returns a cell array with all the strings in alphabetical order. (5 pts) 3. The function should ignore whether letters are lower case or upper case. (2 pts) Test your function with the following: (2 pts) >> sorted=sortWords({’Hello’,’hell’,’abc’,’aa’,’aza’,’aab’,’AaBb’,’a’}) sorted = ’a’ ’aa’ ’aab’ ’AaBb’ ’abc’ ’aza’ ’hell’ ’Hello’ Note: Your function may...
This is python: #Write a function called count_positive_evens. This function #should take as input a list...
This is python: #Write a function called count_positive_evens. This function #should take as input a list of integers, and return as #output a single integer. The number the function returns #should be the count of numbers from the list that were both #positive and even. # #For example: # # count_positive_evens([5, 7, 9, 8, -1, -2, -3]) -> 1 # count_positive_evens([2, 4, 6, 8, 10, 12, 15]) -> 6 # count_positive_evens([-2, -4, -6, -8, -10, 1]) -> 0 # #0...
USING PYTHON, write a function that takes a list of integers as input and returns a...
USING PYTHON, write a function that takes a list of integers as input and returns a list with only the even numbers in descending order (Largest to smallest) Example: Input list: [1,6,3,8,2,5] List returned: [8, 6, 2]. DO NOT use any special or built in functions like append, reverse etc.
Exercise 4 – Lists and input Using a function Write a program that prompts the user...
Exercise 4 – Lists and input Using a function Write a program that prompts the user to input a sequence of words. The program then displays a list of unique words (words that only occurred once, i.e. no repeated).
Task 1: [10 Marks] Write a function “reverse” in your queue class (linked list implementation) that...
Task 1: [10 Marks] Write a function “reverse” in your queue class (linked list implementation) that reverses the whole queue. In your driver file (main.cpp), create an integer queue, push some values in it, call the reverse function to reverse the queue and then print the queue.
Your task in this exercise is to write a function called get_playlist_tracks(database_filename, which_playlist), which formulates a...
Your task in this exercise is to write a function called get_playlist_tracks(database_filename, which_playlist), which formulates a SELECT statement that returns track name, album title, genre name, artist name and track composer for all tracks in the database which are associated with a given playlist. This statement requires several nested INNER JOINs to pull together data from the "tracks", "albums", "genres", "artists", and "playlists" table. The default ordering of tracks should be used. The result of the function is a list...
Write a function in any functional programming language that will reverse a general list. For example,...
Write a function in any functional programming language that will reverse a general list. For example, if an input is (A (B C (D E)) F), output is (F ((E D) C B) A).  Please note that any built-in/pre-defined function, e.g., reverse, cannot be used in your answer. Please DO NOT hard-code any input values, output values in your code. Please submit a screenshot of where your code got compiled, executed, showing the execution result
Write a function that takes a binary search tree as input and produces a linked list...
Write a function that takes a binary search tree as input and produces a linked list of the entries, with the entries sorted (smallest entries at the front of the list and largest entries at the back). *Hint: use in-order traversal.* C++ there is no any structure
Write a function cube_all_lc(values) that takes as input a list of numbers called values, and that...
Write a function cube_all_lc(values) that takes as input a list of numbers called values, and that uses a list comprehension to create and return a list containing the cubes of the numbers in values (i.e., the numbers raised to the third power). This version of the function may not use recursion.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT