Computer Science, asked by JAMPANAGANGOTHRI, 3 months ago

Given a list of integers and a value k, you are required to print an integer which appears in the list exactly k
times.It is guaranteed that only one integer repeats k times. If k times integer not found display “Integer Not
Found”

Answers

Answered by anindyaadhikari13
6

\textsf{\large{\underline{Solution}:}}

The given problem is solved using language - Python.

a=list()

i=0

found=False

n=int(input('How many elements for the list? '))

print('Enter the elements...')

for i in range(n):

   a.append(int(input('> ')))

b=set(a)

k=int(input('Enter value of k: '))

for i in b:

   if a.count(i)==k:

       print(i,'occured exactly',k,'times.')

       found=True

if not found:

   print('Integer not found.')

\textsf{\large{\underline{Logic}:}}

  • Accept the list and the value of k from the user.
  • Use list‎.‎‎count() method to count the frequency of element.
  • If both the frequency and k are equal, display the element.

\textsf{\large{\underline{Sample I/O}:}}

#1

How many elements for the list? 5

Enter the elements...

> 1

> 2

> 2

> 3

> 4

Enter value of k: 2

2 occured exactly 2 times.

#2

How many elements for the list? 5

Enter the elements...

> 1

> 2

> 3

> 4

> 5

Enter value of k: 2

Integer not found.

Attachments:
Similar questions