Question

In: Computer Science

Q1. Write an algorithm to do the following operation on an array passed as parameter: If...

Q1. Write an algorithm to do the following operation on an array passed as parameter: If an element of the array is having more than one occurrence, then keep the first occurrence and remove all other occurrences of the element.

Solutions

Expert Solution

Please find the answer below.

There can be two types of array

1) Sorted array : { 1, 2, 3 , 4, 5, 5 }

2) Unsorted array : { 1, 4, 5, 2, 6, 4 }

1) Algorithm to remove duplicate elements in a sorted array:

  • Input the number of elements of the array (n).
  • Input the array (arr) elements.
  • Pass the array (arr) and the size of the array (n) into the function as parameters.
  • Function :
    • Loop i = 0 to n-1
      • if (arr[i] != arr[i+1])
      • temp[j++] = arr[i]
      • temp[j++] = arr[n-1]
    • Loop i = 0 to j-1
      • arr[i] = temp[i]
    • return j
  • Loop i= 0 to j-1
    • Print : arr[i]

2) Algorithm to remove duplicate elements in an unsorted array :

  • Input the number of elements of the array (n).
  • Input the array elements (arr).
  • Pass the array (arr) and the size of the array (n) into the function as parameters.
  • Function :
    • Create a hashmap and store all the elements and their count.
    • Print all the elements from the hashmap having count = 1.

Related Solutions

Q1) Write a program to implement the quick sort algorithm in a one dimensional array? Q2)...
Q1) Write a program to implement the quick sort algorithm in a one dimensional array? Q2) Write a program to implement the merge sort algorithm in a one dimensional array?
write a program in C Write a function that is passed an array of characters containing...
write a program in C Write a function that is passed an array of characters containing letter grades of A, B, C, D, and F, and returns the total number of occurrences of each letter grade. Your function should accept both lower and upper case grades, for example, both 'b' and 'B' should be bucketed into your running total for B grades. Any grade that is invalid should be bucketed as a grade of 'I' for Incomplete. You must use...
Write a method which is passed A[], which is an array of int, and an int...
Write a method which is passed A[], which is an array of int, and an int passingScore. The method returns the number of items in A[] which are greater than or equal to passingScore. Write a method which is passed an array of int A[]. The method returns true if A[] is the same backwards and forwards. Write a method same( ), which is passed two arrays of int. The method returns true if the two arrays contain exactly the...
Answer the following questions(5pt) a)   Write an algorithm that finds the largest item in an array...
Answer the following questions(5pt) a)   Write an algorithm that finds the largest item in an array of n items by using divide-and-conquer algorithm design pattern. b)   Analyze the worst-case time complexity of your algorithm in (a), and show the results using asymptotic notation (ϴ)
Write a function in C# that takes an array of double as the parameter, and return...
Write a function in C# that takes an array of double as the parameter, and return the average
In Java: Write a method called copy, which is passed A[], which is an array of...
In Java: Write a method called copy, which is passed A[], which is an array of int, and an integer n. The method returns a new array consisting of the first n items in A[]. Write a method called slice, which is passed A[], which is an array of int, an integer i and an integer j. The method returns a new array consisting of all of the items in A from A[i] to A[j] inclusive.
Write an algorithm which has a pre-populated array an array_input of array type and separates it,...
Write an algorithm which has a pre-populated array an array_input of array type and separates it, every other character, into two separate arrays called array1 and array2.
Q1. Write a Java program to do sequential search to find element 55 in array 10,20,35,45,55,65,75,85....
Q1. Write a Java program to do sequential search to find element 55 in array 10,20,35,45,55,65,75,85.                                                                                                                                                                    Q2.Write a Java program to find element 54 in array 45,41,65,53,76,90 using Binary Search. (Hint: Binary search is applied to sorted array elements) Q4.   Write a java program to create array list subject        - add English, Maths, Science to the list        - add Computers at index 2        - display first occurrence index of Maths        - traverse the list using...
[15 marks] (GetPositiveNumbers.java) Write a method that receives an array of integers as a parameter. The...
[15 marks] (GetPositiveNumbers.java) Write a method that receives an array of integers as a parameter. The method finds and returns an array which contains the positive numbers. For example, if the method receives an array with the following elements: 0 3 -1 2 5 1 -5 2 -2 0 the method should return an array with the following elements: 3 2 5 1 2 In the main method, randomly generate an array of 10 random integers between -5 and 5,...
C++ Write a function called linearSearch that takes an array as a parameter and search for...
C++ Write a function called linearSearch that takes an array as a parameter and search for a specific value inside this parameter. The function returns the frequency of a specific value in the array. In the main function: 1. Define an array called salaries of length 5. 2. Initialize the array by asking the user to input the values of its elements. 3. Define a variable called key and ask the user to enter a value for this variable. 4....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT