Challenges-Bitmasking ( Unique number III)

I’m getting 50% correct answer.

here is my code

import java.util.*;

public class Main {

static int bit_set[] = new int[32];

static void add_bits(int n)
{
	int pos = 0;
	while(n>0)
	{
		if((n&1) == 1)
		 bit_set[pos++]++;
		n = n>>1;  
	}
}

static int set_bit(int n, int pos)
{
	int mask = 1<<pos;
	return mask|n;
}

public static void main(String args[]){
    // Your Code Here

	Scanner sc = new Scanner(System.in);
	int n = sc.nextInt();
	int ans = 0;

	while(n-- > 0)
		add_bits(sc.nextInt());

	for(int i=0; i<32; i++)
	{
                if(bit_set[i]%3 == 1)
		  ans = set_bit(ans, i);
	}
	System.out.println(ans);
}

}

for i/p
7
1 1 1 13 5 5 5
ur o/p is 7
desired o/p 13

refer to the below code

Thank you . I got my mistake. In function add_bit I incremented ‘pos’ variable inside if condition which is wrong it should be incremented in outside of if block.

1 Like

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

1 Like