Question

In: Computer Science

Finding duplicate values of an array of integer values. Example 1 Input: arr[] = {5, 2,...

Finding duplicate values of an array of integer values.

Example 1

Input: arr[] = {5, 2, 7, 7, 5}, N = 5

Output:

Duplicate Element: 5

Duplicate Element: 7

Example 2

Input: arr[] = {1, 2, 5, 5, 6, 6, 7, 2}, N = 8

Output:

Duplicate Element: 2

Duplicate Element: 5

Duplicate Element: 6

CODE::

class Main {
  
public static void FindDuplicate(int[] arr){
// write your code to find duplicates here and print those values
  
}
  
public static void main(String[] args) {
// take array size as input from keyboard here
  
  
// take the array elements as input from keyboard here
  
  
}
}

Solutions

Expert Solution

import java.util.Scanner;

class Main {

    public static void FindDuplicate(int[] arr){
        int count;
        for(int i = 0;i<arr.length;i++){
            count=0;
            for(int j = 0;j<i;j++){
                if(arr[i]==arr[j]){
                    count++;
                }
            }
            if(count==0) {
                count = 0;
                for (int j = i+1; j < arr.length; j++) {
                    if (arr[i] == arr[j]) {
                        count++;
                    }
                }
                if(count==1){
                    System.out.println("Duplicate Element: "+arr[i]);
                }
            }
        }

    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        // take array size as input from keyboard here
        int size = scan.nextInt();

        int arr[] = new int[size];

        // take the array elements as input from keyboard here
        for(int i = 0;i<size;i++){
            arr[i] = scan.nextInt();
        }

        FindDuplicate(arr);
    }
}


Related Solutions

2. Define a function max_n(arr, n) that takes in an array and an integer as arguments....
2. Define a function max_n(arr, n) that takes in an array and an integer as arguments. Your function will then return the n largest values from that array as an array containing n elements. It is safe to assume that arr will have at least n elements. The resulting array should have the largest number on the end and the smallest number at the beginning. For Example: max_n(np.array([1,2,3,4,5]), 3) returns np.array([3,4,5]) max_n(np.array([10,9,8,7,6,5]), 4) returns np.array([7,8,9,10]) max_n(np.array([1,1,1]), 2) returns np.array([1,1])
(a) Implement the following algorithm, which is given a duplicate-free array array as input, in C++....
(a) Implement the following algorithm, which is given a duplicate-free array array as input, in C++. whatDoIDo (array): 1) Build a heap from array (using buildHeap as explained in class), where the heap starts at position array[0]. 2) Starting from j = size of array - 1, as long as j>0: i. Swap the entries array[0] and array[j]. ii. Percolate down array[0], but only within the subarray array[0..j-1]. iii. Decrement j by 1. Provide three input/output examples for duplicate-free arrays...
IN JAVA PLEASE Given an unsorted array numbers of integers with duplicate values. Sort the array...
IN JAVA PLEASE Given an unsorted array numbers of integers with duplicate values. Sort the array and remove the duplicates in-place such that each element appears only once in the input array and returns the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. Find the time complexity of your removeDuplicates() method in Big-O notation and write that in a comment line on the top...
Write a statement to call prepare Arr to set values for the array (Using C++) #include...
Write a statement to call prepare Arr to set values for the array (Using C++) #include using namespace std; const int NUM = 10; void prepareArr(int a[]); int countEven (int b[]); int main() { int arr[NUM]; // write a statement to call prepareArr to set values for the array // write a statement to call countEven and print the data returned for(int i = 0; i cout << arr[i] <<" "; cout < return 0; } void prepareArr(int a[]) {...
C Write a function int sort(int** arr, int n) that takes as input an array of...
C Write a function int sort(int** arr, int n) that takes as input an array of int pointers, multiplies the ints the pointers point to by -1, and then sorts the array such that the values pointed to by the pointers are in decreasing order. For example, if the array is size 10 the pointer at index 0 will point to the largest value after multiplying by -1 and the pointer at index 9 will point to the smallest value...
Solve this comp architecture problem: Supposed you have an array int* arr = {3,7,6,4,5}. The values...
Solve this comp architecture problem: Supposed you have an array int* arr = {3,7,6,4,5}. The values in arr store in the memory of 0(x21), 8(x21), .... , 32(x21). Transform the following RISC-V code into a C program. ld x5, 16(x21) addi x6, x0, 3 sll x5, x5, x6 sd x5, 8(x21)
class Arrays1{ public int getSumOfValues(int[] arr){ // return the sum of values of array elements int...
class Arrays1{ public int getSumOfValues(int[] arr){ // return the sum of values of array elements int sum = 0; int i; for(i = 0; i < arr.length; i++){ sum += arr[1]; } return sum; } public int getAverageValueInArray(int[] arr){ // return the average value of array elements int value = 0; for(int i = 0; i < arr.length; i++){ double average = value/ arr.length; } return value; } public int getNumberOfEvens(int[] arr){ //return the number of even values found in...
Problem 3 (4+2+2 marks). (a) Implement the following algorithm, which is given a duplicate-free array array...
Problem 3 (4+2+2 marks). (a) Implement the following algorithm, which is given a duplicate-free array array as input, in C++. whatDoIDo (array): 1) Build a heap from array (using buildHeap as explained in class), where the heap starts at position array[0]. 2) Starting from j = size of array - 1, as long as j>0: i. Swap the entries array[0] and array[j]. ii. Percolate down array[0], but only within the subarray array[0..j-1]. iii. Decrement j by 1. Provide three input/output...
Bubble Sort is performed on an array with values [6, 1, 2, 0, 5, 4]. What...
Bubble Sort is performed on an array with values [6, 1, 2, 0, 5, 4]. What are the array values after two passes of the Bubble Sort? The largest value in the array is moved to the correct location after each pass.
Please Solve with c language Create 5-by-5 integer array. Initialize the elements of the array starting...
Please Solve with c language Create 5-by-5 integer array. Initialize the elements of the array starting from 1. Your element [0][0] should be equal to 1; element[4][4] should be equal 25. Print the array. The output should have 5 rows and 5 columns. Specify the width for each output to demonstrate the table in a formatted view. Change the value of the elements: 2nd row 4th column to 24, 1st row 3rd column to 13. Print the array again. Find...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT