How to find all odd occurring elements in an array in JAVA

find the number occurring odd number of times in an array
find the two strange elements
find the numbers occurring odd number of times in an array
find the odd int
find odd one out in array
odd occurrences in array javascript
find odd numbers in array javascript
find largest odd number array java

I am trying to find all the elements which occur an odd number of times in an array. I worked out a little bit, but my code is only returning the correct answer if there is only one number which occurs odd number of times. If there are two or more odd occurring numbers than I am not able to process it. What I understand is that if we do bit-wise XOR of elements than we get one odd occurring element. How can I improve it for multiple numbers?

Below is my code:

public class OddOccur {
    public int oddoccurence(int[] arr, int size) {
        int res = 0;
        int[] fin = new int[size];

        for (int i = 0; i < size; i++) {
            res = res ^ arr[i];

        }
        return res;
    }

    public static void main(String args[]) {
        int[] arr = { 2, 5, 5, 2, 2, 3, 3, 3 };

        int n = arr.length;
        OddOccur obj = new OddOccur();

        System.out.println("odd occuring element is:"
                + obj.oddoccurence(arr, n));
    }
}

Need help to solve this issue!


public int oddoccurence(int[] arr, int size);

First, there can be multiple numbers that occur an odd number of times. There's no way to write this function and make it work if it only returns a single int. You'll need a function which can return multiple numbers.

public int[] oddOccurrences(int[] array);

Second, don't try to be clever. Using XOR is "clever". You're not going to find a simple solution using XOR. It'll be some convoluted, complicated mess. Keep it simple:

  1. Count the number of times each number appears.
  2. Then find all the odd counts.

Example pseudo-code:

// Map from numbers to counts. Assume the counts start at 0.
Map<Integer, Integer> counts;

for (number in array) {
    counts[number] += 1
}

// List of numbers that occur an odd number of items.
List<Integer> oddNumbers;

for (number, count in counts) {
    if (count % 2 != 0) {
        oddNumbers.add(number);
    }
}

Find the Number Occurring Odd Number of Times, { int[] arr = { 4, 3, 6, 2, 6, 4, 2, 3, 4, 3, 3 }; Find all odd occurring elements in an array having limited range of elements. Given an array having elements between 0 to 31, find elements which occurs odd number of times without using the extra space. For example, Input: { 5, 8, 2, 5, 8, 2, 8, 5, 1, 8, 2 } Output:The odd occurring elements are: 5, 2, and 1.


Maybe an old post.. but here my answer.

In my opinion is not necessary to count the occurrences of a number to determine if it's odd or not. Let's use a data structure like a Set and just add to it the item if is still not prenent or remove it if already there.

Something like this:

public int solution(int[] A){

    Set<Integer> unpaired = new HashSet<Integer>();
    for (int i = 0; i<A.length; i++){
        if (unpaired.contains(A[i])){
            unpaired.remove(new Integer(A[i]));
        }else{
            unpaired.add(A[i]);
        }
    }


    // all printed out values are odd
    for (Integer res : unpaired){
        System.out.println(res);
    } 
}

Find the two numbers with odd occurrences in an unsorted array , Find the Number Occurring Odd Number of Times. Given an array of positive integers. All Duration: 6:28 Posted: May 24, 2012 You need to find the number which occurs odd number of time. You need to solve it with o (n) time complexity and o (1) space complexity. For example: 1. 2. 3. 4. int array[] = new int[]{20, 40, 50, 40, 50, 20, 30, 30, 50, 20, 40, 40, 20}; Number which occurs odd number of times is : 50.


You don't really need to keep track of how many times you've seen each integer, only whether you've seen it an odd number of times. So start with your data structure (map, hash table, dictionary, whatever) being empty, which correctly reflects the state that you have seen no numbers an odd number of times. Now for each number x in the array, if x is in the map then remove it (now you've seen it an even number of times) otherwise add it

How to find all odd occurring elements in an array in JAVA, And the xor of a set will be one of the odd occurring elements. C++; C; Java; Python3; C#; PHP. C++. The outer loop traverses through all elements. The inner loop counts occurrences of the current element. We print the elements whose counts of occurrences are odd. Time complexity if this solution is O (n 2) A better solution is to use hashing. Time complexity of this solution is O (n) but it requires extra space.


Here is my solution using HashMap:

 public int solution(int[] A) {

        HashMap<Integer, Integer> map = new HashMap<>();
        HashSet<Integer> val = new HashSet<>();
        for (int i=0; i<A.length; i++){
        if (map.get(A[i]) == null) {
            map.put(A[i], 1);
        } else map.put(A[i], map.get(A[i])+1);

        if (map.get(A[i])%2 == 1) 
            val.add(A[i]);
            else val.remove(A[i]);
        }

        Iterator<Integer> itr = val.iterator();  
        return itr.next();  

    }

Find all odd occurring elements in an array having limited range of , public int oddoccurence(int[] arr, int size);. First, there can be multiple numbers that occur an odd number of times. There's no way to write this function and make​  Find the sum of all highest occurring elements in an Array; Find array elements with frequencies in range [l , r] Sum of all minimum occurring elements in an Array; Two odd occurring elements in an array where all other occur even times; Find if array can be sorted by swaps limited to multiples of k; GCD of elements occurring Fibonacci number of times in an Array


This solution scored 100% on codility. What we do is simply initialize a HashMap. The lookup time for a HashMap is O(1) constant time, see here for more info. Since we loop through the entire array only once, we end up with O(N) time, where N is the length of the array.

If an element already exists in the HashMap, then we can remove it since we have found its corresponding pair. By the end of this, we should end up with a HashMap with only a single pair. Simply output this number and you are done.

import java.util.*;
class Solution {
    public int solution(int[] A) {
        int key;
        Map<Integer, Integer> unpaired = new HashMap<Integer, Integer>();
        for (int i = 0; i < A.length; i++){
            key  = A[i];
            Integer value = unpaired.get(key);
            if (value != null){
                unpaired.remove(key); // Remove by key
            }
            else{
                unpaired.put(key,0);
            }
        }
        for (Map.Entry<Integer, Integer> entry : unpaired.entrySet())
        {
            key =  entry.getKey();
        }
        return key;
    }
}

Find a Number occurring odd number of times in a Given array , So, the odd occurring elements are 1, 2, and 5 since 1 holds 1st, 2nd, and 5th position. C; Java; Python. C. The task is to find the sum of all odd occurring elements in the given array. That is the sum of all such elements whose frequency is odd in the array. Examples: Input: arr[] = {1, 1, 2, 2, 3, 3, 3} Output: 9 The odd occurring element is 3, and it's number of occurrence is 3. Therefore sum of all 3's in the array = 9. Input: arr[] = {10, 20, 30, 40, 40} Output: 60 Elements with odd frequency are 10, 20 and 30. Sum = 60.


Find the number occuring odd number of times in an array, Objective: Given a array of integers, in which every elements occurs even number of times except one number which occurs add number of times. Find out that  This can be accomplished by looping through the array and printing the elements of an array by incrementing i by 2 till the end of the array is reached. In the above array, the elements which are on odd positions are a, c and e.


Find the number which occurs odd number of times in an Array , Start from first element and loop through till last element of an array. Perform XOR of all elements on an array. XOR from point #1 will be Zero if the number occurs  The Best Solution is to do bitwise XOR of all the elements. XOR of all elements gives us odd occurring element. Please note that XOR of two elements is 0 if both elements are same and XOR of a number x with 0 is x. Below is the implementation of the above approach.


How to find two numbers with odd occurrences in an unsorted array , Find the number which occurs odd number of times in an Array. The appearance of the Duration: 7:31 Posted: Jun 15, 2017 Pick all elements of input array one by one. Look for the picked element in hash table. If the element is found in hash table, increment its count in table. If the element is not found, then enter it in hash table with count as 1. After all elements are entered in hash table, scan the hash table and print elements with odd count.