This a java program for finding duplicates in two string arrays.
import java.util.*;
public class session
{
public static void main(String[] args)
{
int flag=0;
Scanner s = new Scanner(System.in);
System.out.println("Enter the number of students in session 1 and session 2");
int nk= s.nextInt();
int ng= s.nextInt();
String[] ses1 = new String[nk];
String[] ses2 = new String[ng];
System.out.println("Enter the registration nos. for session 1");
for(int i = 0; i < nk; i++)
{
ses1[i] = s.nextLine();
}
System.out.println("Enter the registration nos. for session 2");
for(int i = 0; i < ng; i++)
{
ses2[i] = s.nextLine();
}
for(int i = 0; i < nk; i++)
{
for(int j = 0; j < ng; j++)
{
if(ses1[i].equals(ses2[j]))
{
flag=1;
}
}
}
if(flag==0)
System.out.println("No Duplicates");
else
System.out.println("Error, There are duplicates");
}
}
The first for loop runs for nk-1 cycles not nk cycles. Is there a rule involving multiple for loops?