Question

In: Computer Science

Given a generic array with ‘n’ items (not only integers). Give a solution for checking whether...

Given a generic array with ‘n’ items (not only integers). Give a solution for checking whether there are any duplicate elements in the array or not? You just need to return a boolean (true or false). State the complexity of your solution. Can you come up with another solution that has O(n logn) complexity? Explain.

Solutions

Expert Solution

/*

Please read comments and see the program and output

Approach1: Use Set in java, which always keep unique element,

            We can easily check, duplicate is there or not using .contains(element) method

            It works in O(1) time comlexity due to internal HashMap implementation.

            So this is best approach.

Approach2: Use ArrayList in java, which runs its complexity with O(n) for contains(element)

           So any element which is there or not, totally it would take O(n*n) time

Approach3: Sort element and use any collections, like ArrayList, using single iteration,

            make comparision like

                currelement == prevElement

            then duplicate is there

            otherwise "There is no duplicate element"

            Since best sorting approach time complexity is O(nlogn)

            and single iteration time complexity is O(n),

            Hence total time complexity would be O(nlogn) + O(n) equals to O(nlogn)

*/

import java.util.*;

class Duplicate {

    //Approach 1, using Set

    static boolean isDuplicateUsingSet(int [] array) {

        Set<Integer> set = new HashSet<Integer>();

        for(int i: array)

            if(set.contains(i))

                return true;

            else

                set.add(i);

        return false;

    }

    //Approach 2, Using ArrayList

    static boolean isDuplicateUsingArrayList(ArrayList<Integer> array) {

        //Check for "one"

        for(int i: array) {

            if(array.contains(2))

                return true;

        }

        return false;

    }

    public static void main(String arg[]) {

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

        list.add(11);

        list.add(2);

        list.add(3);

        list.add(11);

        Set<String> stringArray = new HashSet<String>();

        stringArray.add("one"); //Gives true means unique element,

        stringArray.add("two"); ////Gives true means unique element,

        stringArray.add("three"); // Gives false means element is duplicate

        int a[] = {1,33,2,1};

        System.out.println("int array is " );

        for(int i:a)

            System.out.print(i+" ");

        System.out.println();

        System.out.println("Duplicate is in array " + isDuplicateUsingSet(a));

        System.out.println("Arraylist element is " );

        for(int i:list)

            System.out.print(i + " ");

        System.out.println();

        System.out.println("Duplicate is in arrayList " + isDuplicateUsingArrayList(list));

    }

}

/* Output*/

/* Similary for any generic object , this thing can be done in same manner*/

/* Thanks*/


Related Solutions

A)Given a generic array with ‘n’ items (not only integers). Give a solution for checking whether...
A)Given a generic array with ‘n’ items (not only integers). Give a solution for checking whether there are any duplicate elements in the array or not? You just need to return a boolean (true or false). State the complexity of your solution. Can you come up with another solution that has O(n logn) complexity? Explain. B) Now consider the same problem as Question A, but this time you only have positive integer numbers ranging from 0 to (n-1) in the...
Problem 1: [10 Marks] Given a generic array with ‘n’ items (not only integers). Give a...
Problem 1: [10 Marks] Given a generic array with ‘n’ items (not only integers). Give a solution for checking whether there are any duplicate elements in the array or not? You just need to return a boolean (true or false). State the complexity of your solution. Can you come up with another solution that has O(n logn) complexity? Explain. Problem 2: [10 Marks] Now consider the same problem as problem 1, but this time you only have positive integer numbers...
We have an array A of size n. There are only positive integers in this array....
We have an array A of size n. There are only positive integers in this array. Note that the array may have integers that are not distinct, and it could be any array of positive integers in creation (I like the numbers found the decimal expansion of π for instance). When possible provide the exact complexity of the algorithm. If it’s not possible explain the O/Ω/Θ complexity. a. Design an efficient algorithm to find the maximum difference between any two...
We have an array A of size n. There are only positive integers in this array....
We have an array A of size n. There are only positive integers in this array. Note that the array may have integers that are not distinct, and it could be any array of positive integers in creation (I like the numbers found the decimal expansion of π for instance). When possible provide the exact complexity of the algorithm. If it’s not possible explain the O/Ω/Θ complexity. a. Design an efficient algorithm to find the maximum difference between any two...
1. Given an array of integers a dimension n. If the array contains the same number...
1. Given an array of integers a dimension n. If the array contains the same number of even and odd elements get (a1 + an) (a2 + an-1) ... 2. Given an array of integers dimension n. All array elements with even numbers preceding the first element to the maximum, multiplied by the maximum. 3. Given an array of dimension n. Insert after each zero element of the element in the middle (or the amount of secondary elements for even...
Given an array A of n integers, describe an O(n log n) algorithm to decide if...
Given an array A of n integers, describe an O(n log n) algorithm to decide if the elements of A are all distinct. Why does your algorithm run in O(n log n) time?
1. We have an array A of size n. There are only positive integers in this...
1. We have an array A of size n. There are only positive integers in this array. Note that the array may have integers that are not distinct, and it could be any array of positive integers in creation (I like the numbers found the decimal expansion of π for instance). When possible provide the exact complexity of the algorithm. If it’s not possible explain the O/Ω/Θ complexity. Design an efficient algorithm to find the maximum difference between any two...
1. We have an array A of size n. There are only positive integers in this...
1. We have an array A of size n. There are only positive integers in this array. Note that the array may have integers that are not distinct, and it could be any array of positive integers in creation (I like the numbers found the decimal expansion of π for instance). When possible provide the exact complexity of the algorithm. If it’s not possible explain the O/Ω/Θ complexity. Design an efficient algorithm to find the maximum difference between any two...
java Given a sequence of integers as an array, determine whether it is possible to obtain...
java Given a sequence of integers as an array, determine whether it is possible to obtain a strictly increasing sequence by removing no more than one element from the array. Here are the constraints. 1. You must use only arrays. 2. You are not allowed to make copies of the array (use only the one that is passed). 3. Your algorithm running time must be at most O(n 2 ). i.e. You can have only one loop within another. Ideally,...
Given an unsorted array A of size N of integers, find a continuous sub-array which adds...
Given an unsorted array A of size N of integers, find a continuous sub-array which adds to the given number. Declare dynamic arrays and use only pointers syntax. (no [ ]'s or (ptr+1) stuff. input will be the number of input values to enter followed by the sum to compare with. print out the continuous sub-array of values that are equal to sum or the message 'no sum ofund.' there may be more than one sub-array to be found in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT