Prepin
Log in

Create a program to identify integers in a specified list that appear an even number of times.

Approach / Explanation

This task requires you to create a program that accepts a list of integers and returns all the numbers that appear an even number of times within that list. It is crucial to focus on the specifics: The numbers you return must have even occurrences, meaning they should appear 2, 4, 6 times, etc. Your output should include all such numbers from the provided list. To tackle this problem: Consider utilizing a hash map or dictionary to keep track of how many times each number appears in the list. Iterate through the list of integers and for each number, either add it to the dictionary or update its count. Then, traverse the dictionary to collect all numbers that have even frequencies.

Suggested Answer

In Python, your code may resemble the following: def find_even_occurrences ( lst ) : # initializing an empty dictionary count_dict = { } # increasing count for each number for num in lst : if num in count_dict : count_dict [ num ] += 1 else : count_dict [ num ] = 1 # identifying numbers with even counts even_occurrences = [ num for num , count in count_dict . items . items ( ) if count % 2 == 0 ] return even_occurrences In this implementation, lst represents the list of numbers. The find_even_occurrences function constructs a dictionary that associates each number with its frequency, then performs another iteration over this dictionary to extract all numbers that have an even frequency.

Alternative Answer

Here is another example written in Java: import java . util . * ; public class Main { static void printEvenOccurences ( int arr [ ] ) { HashMap < Integer , Integer > countMap = new HashMap < Integer , Integer > ( ) ; // counting occurrences of array elements for ( int i = 0 ; i < arr . length ; i ++ ) { if ( countMap . get ( arr [ i ] ) == null ) { countMap . put ( arr [ i ] , 1 ) ; } else { countMap . put ( arr [ i ] , countMap . get ( arr [ i ] ) + 1 ) ; } } // print numbers with even occurrences for ( Map . Entry < Integer , Integer > entry : countMap . entrySet ( ) ) { if ( entry . getValue ( ) % 2 == 0 ) { System . out . println ( entry . getKey ( ) ) ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 2 , 4 , 5 , 2 , 3 , 1 , 6 , 6 } ; printEvenOccurences ( arr ) ; } } In this illustration, the printEvenOccurences method establishes a HashMap to track the frequency of each number, and subsequently outputs the numbers that occur an even number of times.

Question Details

Difficulty & Category

Medium

Companies

Microsoft

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.