Finding duplicate elements in an array without using nested loops [duplicate] - java

This question already has answers here:
Find duplicate element in array in time O(n)
(24 answers)
Closed 7 years ago.
I recently had an interview which consisted of following problem. Please help with possible solutions.
Write a method in Java to find duplicate elements in an integer array without using nested loops ( for/ while / do while, etc ) and without using library functions or standard API's.

Hey the below solution has complexity O(n) and works fine. Check if it helps.
public class Main {
public static void main(String[] args) {
int a[] = new int[]{10,3,5,10,5,4,6};
String distinctElement="";
String repetitiveTerms="";
for(int i=0;i<a.length;i++){
if(i==0){
distinctElement+=a[i]+" ";
}
else if(distinctElement.contains(""+a[i])){
repetitiveTerms+=a[i]+" ";
}
else{
distinctElement+=a[i]+" ";
}
}
}
}

Related

what code can i use to clear the screen(java)? [duplicate]

This question already has answers here:
commands in java to clear the screen
(11 answers)
Closed 5 years ago.
I'm writing a text-based unit converter and I want to be able to run a clear command so that the window that the program I want to know how I can do it.
I think you can System.exec("clear") but that depends on what operating system the program is running on.
This is my choice for clear:
public static void clearConsole() {
String value = "\n\r";
for (int i = 0; i < 5; ++i) {
value = value + value;
System.out.printf(value);
}
}

fun(agr++) vs fun(arg+1) in java [duplicate]

This question already has answers here:
How do the post increment (i++) and pre increment (++i) operators work in Java?
(14 answers)
Closed 6 years ago.
I was trying to reverse String using recursion as follows.
But when I am calling reverse(arr,start++,end--) it is giving stackoverflow error.
I tried using reverse(arr,start+1,end-1) then it is working fine.
public class Reverse {
public static void main(String[] args) {
char inp[] = "TestString".toCharArray();
int n = inp.length;
reverse(inp, 0,n-1);
System.out.println(String.valueOf(inp));
}
public static void reverse(char[] arr, int start, int end){
if(start>=end)
return ;
char tmp;
tmp = arr[start];
arr[start] = arr[end];
arr[end] = tmp;
reverse(arr,start+1,end-1);//error line
}
What is the problem with reverse(arr,start++,end--)?
I want to know why value of start++ and end-- will not get passed to function.
Have a look at this SO question
SO post-increment-and-pre-increment-concept
Post
reverse(arr,start++,end--) when you do this the incremented/decremented value will not be passed to recursive method, means you are calling the method with original value of start and end result in SO error
Pre
When you do reverse(arr,start+1,end-1) or reverse(arr,++start,--end) incremented and decremented value of start and end will be passed to recursive method.
While debugging in Eclipse IDE check the values of start and end in variables panel if not using IDE write start and end values to console in reverse method

Trouble with Arrays in Java and for loops [duplicate]

This question already has answers here:
Possible lossy conversion from double to int
(3 answers)
Closed 6 years ago.
I keep getting an error but Im not understanding why. The error is about not being able to convert from double to int but I have everything declared in double so I dont understand why im getting this error.
class ConeArray{
public static void main(String[] args) {
double[] coneArray;
for(double i = 0; i < coneArray.length; i++) {
coneArray = Math.PI * Math.pow((i*2),2) * (1/3 * (4 * i));
System.out.println("Volume of cone: " + coneArray[i]);
}
}
}
You have declared double i in your for loop. Array indexes must be ints.
Also, I don't see you actually instantiating the array in the code sample you provided. The current implementation will throw a NPE if you try to access it.

How to overwrite and print the values in console in java [duplicate]

This question already has answers here:
How can I return to the start of a line in a console?
(3 answers)
Closed 7 years ago.
Here is one while loop that will iterate 11 times.
public static void main(String[] args) {
int count=0;
while(count<=10){
System.out.print(count);
count++;
}
}
& the output will be definitely : 012345678910
But I want to display the same output in such a way that every iteration will overwrite the previous value while printing the value on console.
Here's the restriction is : We can not use file.
clearing the console on every iteration can be one of the ways, is there anything left we can do?
You can use \r which returns to the start of a line: System.out.print("\r" + count); should work.

Why does my recursive factorial method always return 0? [duplicate]

This question already has answers here:
Why does Java think that the product of all numbers from 10 to 99 is 0?
(9 answers)
Closed 8 years ago.
I have created a recursive method to calculate the facortial of a number, however, it is always returning 0, and I can not see why. I have provided my code below:
public class projectTwenty {
public static void main(String [] args) {
int factorialAns = factorial(100);
System.out.println(factorialAns);
}
private static int factorial(int n) {
if (n == 0) {
return 1;
}else {
return (n * factorial(n-1));
}
}
}
I have tried changing the way in which the function is called and how values are returned, no luck so far.
I have also searched Google/StackOverflow for similar methods/questions and am yet to find a solution.
Because 100 factorial has so much digits that it causes an overflow on the integer type. You can try it on a smaller values and it will work much better.
In case you want to actually calculate big factorials you can use the BigInteger class in java.
factorial(100) is probably too large to fit in an int and you get an overflow. For smaller values of n it works fine.
12 is the highest int for which factorial(12) won't overflow.

Categories

Resources