First non-repeating character geeks solution in java
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
public static void main (String[] args)
{
Scanner ab=new Scanner(System.in);
int t=ab.nextInt();
while(t>0)
{
int n=ab.nextInt();
int arr[]=new int[500];
int order[]=new int[26];
int p=0;
while(p<n)
{
arr[p]=(int)(ab.next().charAt(0)-97); //to hold the order
order[arr[p]]++;//to couunt occurence of order
int i=0;
boolean flag=false;
while(i<=p)
{
if(order[arr[i]]==1)
{
System.out.print(Character.toString ((char) (arr[i]+97))+" ");
flag=true;
break;
}
i++;
}
if(!flag)
System.out.print("-1 ");
p++;
}
t--;
System.out.println("");
}
}
}
0 Comments:
Post a Comment