How to avoid nested loop in this program | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to avoid nested loop in this program

I want to make the program more efficient import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner ob =new Scanner(System.in); String s=ob.nextLine(); int c=0; for(int i=0;i<s.length()-1;i++) { for(int j=(i+1);j<s.length();j++) { if(s.charAt(i)==s.charAt(j)) { c++;break; } } } if(c>0) System.out.println("Deja Vu"); else System.out.println("Unique"); } }

31st Mar 2020, 4:32 PM
Shubham Prashant
Shubham Prashant - avatar
1 Answer
+ 1
You can try to do this way. With boolean array. Pretty simple solution and you can avoid double for loop. https://code.sololearn.com/c6iDFjEgbmTu/?ref=app
31st Mar 2020, 9:26 PM
Michal
Michal - avatar