TCS Coding Questions 2022 Day 2 Slot 1

Coding Question 2 for 2022 (September slot)

In this article, we will discuss about the TCS Coding Question which is asked in the TCS placement test. This type of Coding Questions will help you to crack your upcoming TCS exam as well as during your inteview process.

tcs nqt coding question

 

TCS Coding Question Day 2 Slot 1 – Question 2

At a fun fair, a street vendor is selling different colours of balloons. He sells N number of different colours of balloons (B[]). The task is to find the colour (odd) of the balloon which is present odd number of times in the bunch of balloons.

Note: If there is more than one colour which is odd in number, then the first colour in the array which is present odd number of times is displayed. The colours of the balloons can all be either upper case or lower case in the array. If all the inputs are even in number, display the message “All are even”.

Example 1:

  • 7  -> Value of N
  • [r,g,b,b,g,y,y]  -> B[] Elements B[0] to B[N-1], where each input element is sepārated by ṉew line.

Output :

  • r -> [r,g,b,b,g,y,y]  -> “r” colour balloon is present odd number of times in the bunch.

Explanation:

From the input array above:

  • r: 1 balloon 
  • g: 2 balloons
  • b:  2 balloons
  • y : 2 balloons 

Hence , r is only the balloon which is odd in number.

Example 2:

Input:

  • 10 -> Value of N
  • [a,b,b,b,c,c,c,a,f,c] -> B[], elements B[0] to B[N-1] where input each element is separated by new line.

Output :

b-> ‘b’ colour balloon is present odd number of times in the bunch.

Explanation:

From the input array above:

  • a: 2 balloons
  • b: 3 balloons 
  • c: 4 balloons 
  • f: 1 balloons 

Here, both ‘b’ and ‘f’ have odd number of balloons. But ‘b’ colour balloon occurs first.

Hence , b is the output.

Input Format for testing

The candidate has to write the code to accept: 2 input 

  • First input: Accept value for number of N(Positive integer number).
  • Second Input : Accept N number of character values (B[]), where each value is separated by a new line.

Output format for testing

The output should be a single literal (Check the output in example 1 and example 2)

Constraints:

  • 3<=N<=50
  • B[i]={{a-z} or {A-Z}}

16 comments on “TCS Coding Questions 2022 Day 2 Slot 1”


  • Srivalli

    Python code
    n=int(input())
    B=[input() for _ in range(n)]
    C=[]
    for i in range(n):
    if (B.count(B[i])%2)!=0:
    C.append(B[i])
    if len(C)==0:
    print(“All are equal”)
    else:
    print(C[0])


  • kimaya

    data_dict = dict()

    variable = [“r”,”g”,”b”,”b”,”g”,”y”,”y”]
    val_key = None

    for data in variable:
    if data not in data_dict.keys():
    data_dict[data] = 1
    else:
    data_dict[data] += 1

    for key,value in data_dict.items():
    if value%2 != 0:
    val_key = key

    print(val_key)


  • Venkatesh

    N = int(input())
    li = list(input().split())
    li1 = []
    li2 = []
    for i in li:
    c = li.count(i)
    if(c%2 != 0):
    li1.append(i)
    else:
    li2.append(i)
    lent = len(li1)
    if(lent >= 1):
    print(li1[0])
    if(li2 == li):
    print(“All are Even”)


  • Ravi

    #include
    using namespace std;
    int main(){
    int n;
    cin>>n;
    mapump;
    vectorv;
    for(int i=0;i>x;
    v.push_back(x);
    ump[x]++;
    }
    int minindex=INT_MAX;
    for(auto it:ump)
    {
    if( (it.second) %2!=0)
    {
    for(int i=0;i<n;i++)
    {
    if(v[i]==it.first)
    minindex=min(minindex,i);
    }
    }

    }
    cout <<v[minindex]<<endl;
    }


  • Bittu

    N = int(input())
    ballon_list = []
    count_list = []
    for i in range(N):
    ele = input().lower()
    ballon_list.append(ele)
    ballon_list_2 = list(set(ballon_list))
    for i in ballon_list_2:
    count = ballon_list.count(i)
    count_list.append(count)
    for i in count_list:
    if i % 2 != 0:
    index = count_list.index(i)
    break
    print(ballon_list_2[index])


  • Dpa

    n=int(input())
    ballons=[input().lower() for i in range(n)]
    b_count={}
    count=0
    res=[]

    for i in ballons:
    x=b_count.keys()
    if(i not in x):
    b_count[i]=1
    else:
    b_count[i]+=1

    for i,j in b_count.items():
    if(j%2==0):
    count+=1
    else:
    res.append(i)

    if(count==len(b_count.keys())):
    print(“All are even”)
    else:
    print(*res, sep = ‘\n’)


  • 21mmc027

    n=int(input())
    lst_balloons=[]
    for i in range(n):
    lst_balloons.append(input())
    for i in lst_balloons:
    s=lst_balloons.count(i)
    if s%2!=0:
    print(i)
    break


  • SREE

    n=int(input())
    B=list(map(str,input().split()))
    C=[]
    for i in range(n):
    if (B.count(B[i])%2)!=0:
    C.append(B[i])
    if len(C)==0:
    print(“All are equal”)
    else:
    print(C[0])


  • 19_349

    N=int(input())
    l=list(map(str,input().split()))
    for i in l:
    if l.count(i)%2 !=0:
    print(i)
    break


  • siddharthabhunia2020

    #include
    using namespace std;

    int main()
    {
    int n, flag = 0;
    cin >> n;
    char arr[n];
    for (int i = 0; i > temp;
    arr[i] = temp;
    }

    unordered_map m;
    for (int i = 0; i < n; i++)
    {
    m[arr[i]]++;
    }

    for (int i = 0; i < n; i++)
    {
    if (m[arr[i]] & 1)
    {
    cout << arr[i];
    flag = 1;
    break;
    }
    }
    if (flag == 0)
    {
    cout << endl
    << " All are even" << endl;
    }

    return 0;
    }


  • Vishnukant

    x=int(input())
    y=list(input().split())
    m=0
    n=[]
    #creating unique element list
    for i in y:
    if i not in n:
    n.append(i)

    #for cheking how much even element in list
    for i in n:
    if y.count(i)%2==0:
    m=m+1

    if m==len(n):
    print(“All even”)
    exit()

    for j in n:
    if y.count(j)%2!=0:
    print(j,end=” “)


  • SUBHRADIP

    // c++ code by subhradip barik
    #include
    using namespace std;
    int main()
    {
    int size = 10;
    vector odd;
    map data;
    for(int i=0;i> in;
    data[in]++;
    }
    for(auto pr : data)
    {
    cout<< pr.first<<" "<<pr.second<<endl;
    if(pr.second % 2 != 0)
    {
    odd.push_back(pr.first);
    }
    }
    if(odd.size()== 0)
    {
    cout<<"all are even"<<endl;
    return 0;
    }
    cout<<odd[0]<<endl;
    return 0;

    return 0;
    }


    • Keshav

      #include
      using namespace std;

      int main() {
      int t;
      cin >> t;
      while(t–) {
      int d[122] = {0}, n;
      cin >> n;
      char b[n];
      for(int i = 0; i > b[i];
      d[b[i]]++;
      }
      for(int i = 0; i < n; i++)
      if(d[b[i]] & 1) {
      cout << b[i] << endl;
      break;
      }
      }
      return 0;
      }


      • Keshav

        T = int(input())
        for _ in range(T):
        n = int(input())
        b = input().split()
        d = {}
        for e in b:
        d[e] = 1 if not d.get(e) else d[e] + 1
        for e in b:
        if d[e] & 1:
        print(e)
        break


    • Anil

      n=int(input())
      l=list(map(str,input().split()))
      x=[]
      for i in l:
      if i not in x:
      x.append(i)
      dict={}
      for i in x:
      count=0
      for j in l:
      if i==j:
      count=count+1
      dict[i]=count
      max=-1
      for i in x:
      if dict[i]%2!=0 and dict[i]>max:
      max=dict[i]
      for i in x:
      if max==dict[i]:
      print(i)
      break