Exceptionally odd

Exceptionally odd

Difficulty: BasicAccuracy: 50.53%Submissions: 85K+Points: 1

Given an array of N positive integers where all numbers occur even number of times except one number which occurs odd number of times. Find the exceptional number.

Example 1:

Example 2:

Your Task: You don't need to read input or print anything. Your task is to complete the function getOddOccurrence() which takes arr[] and as input parameters and returns the exceptional number.

Expected Time Complexity: O(N) Expected Auxiliary Space: O(1)

Constraints:

1 ≤ N ≤ 105

1 ≤ arr[i] ≤ 106


How to approach:

Here, we need to count the number of occurrences for each item in the arr. And whichever is odd, we will return that item.

to count the number of occurences, we will use: freq[ item ] = freq.get(item, 0) + 1. This will store item and its count in a dict.

and then, we will extract and check for odd counts and return the item:

To view or add a comment, sign in

Others also viewed

Explore topics