mirror of https://github.com/evanferrao/dsa
improve UniqueArrayElement.java
This commit is contained in:
parent
154e6a0e61
commit
aba14d4029
Binary file not shown.
|
|
@ -11,9 +11,27 @@ class UniqueArrayElement{
|
|||
for(int i=0;i<n;i++){
|
||||
arr[i] = sc.nextInt();
|
||||
}
|
||||
System.out.println("The unique element in the array is "+findUnique(arr));
|
||||
sc.close();
|
||||
// is there any way to show the time taken by each function to find the unique element in the array?
|
||||
// yes, we can use the System.currentTimeMillis() function to get the current time in milliseconds
|
||||
// we can use this function to get the time before and after the function call
|
||||
// then we can subtract the two times to get the time taken by the function to execute
|
||||
// can you rewrite the above code to show the time taken by each function to find the unique element in the array?
|
||||
// write the code to show the time taken by each function to find the unique element in the array
|
||||
long start = System.currentTimeMillis();
|
||||
System.out.println("The unique element in the array is "+findUniqueUsingXOR(arr));
|
||||
long end = System.currentTimeMillis();
|
||||
System.out.println("Time taken by findUnique function is "+(end-start)+" milliseconds");
|
||||
start = System.currentTimeMillis();
|
||||
System.out.println("The unique element in the array is "+findUniqueUsingMapping(arr));
|
||||
end = System.currentTimeMillis();
|
||||
System.out.println("Time taken by findUniqueUsingMapping function is "+(end-start)+" milliseconds");
|
||||
start = System.currentTimeMillis();
|
||||
System.out.println("The unique element in the array is "+findUniqueBruteforce(arr));
|
||||
end = System.currentTimeMillis();
|
||||
System.out.println("Time taken by findUniqueBruteforce function is "+(end-start)+" milliseconds");
|
||||
}
|
||||
public static int findUnique(int[] arr){
|
||||
public static int findUniqueUsingXOR(int[] arr){
|
||||
int res = 0;
|
||||
for(int i=0;i<arr.length;i++){
|
||||
res = res^arr[i];
|
||||
|
|
@ -33,4 +51,51 @@ class UniqueArrayElement{
|
|||
// res = 7^3 = 4
|
||||
// return 4
|
||||
|
||||
// write another function using hasing technique to find the unique element in the array
|
||||
// write using mapping technique to find the unique element in the array
|
||||
// DO NOT USE XOR
|
||||
// make another array of size of the largest element in the array and then map the elements of the array to the new array
|
||||
// then find the element which is mapped to 1
|
||||
// return that element
|
||||
// return -1 if no such element is found
|
||||
// write the code for the above logic
|
||||
static int findUniqueUsingMapping(int[] arr){
|
||||
int max = Integer.MIN_VALUE;
|
||||
for(int i=0;i<arr.length;i++){
|
||||
if(arr[i]>max){
|
||||
max = arr[i];
|
||||
}
|
||||
}
|
||||
int[] map = new int[max+1];
|
||||
for(int i=0;i<arr.length;i++){
|
||||
map[arr[i]]++;
|
||||
}
|
||||
for(int i=0;i<map.length;i++){
|
||||
if(map[i]==1){
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
// write a bruteforce method to find the unique element in the array
|
||||
// compare each element with all the other elements in the array
|
||||
// if the element is not found in the array then return that element
|
||||
// return -1 if no such element is found
|
||||
static int findUniqueBruteforce(int[] arr){
|
||||
for(int i=0;i<arr.length;i++){
|
||||
boolean found = false;
|
||||
for(int j=0;j<arr.length;j++){
|
||||
if(i!=j && arr[i]==arr[j]){
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(!found){
|
||||
return arr[i];
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue