I have a Google foobar challenge:
Write a function called answer(data, n) that takes in a list of less than 100 integers and a number n, and returns that same list but with all of the numbers that occur more than n times removed entirely. The returned list should retain the same ordering as the original list - you don't want to mix up those carefully-planned shift rotations! For instance, if data was [5, 10, 15, 10, 7] and n was 1, answer(data, n) would return the list [5, 15, 7] because 10 occurs twice, and thus was removed from the list entirely.
And this was my answer:
public static int[] solution(int[] data, int n) {
// Your code here
int count,c=0;
int flag[]=new int[1000];
int b[]=new int[data.length];
for(int i=0;i<data.length;i++)
{ count=0;
if(flag[(data[i])]==0)
{
for(int j=0;j<data.length;j++)
{
if(data[i]==data[j])
count++;
}
if(count>n)
flag[(data[i])]=1;
else
{
flag[(data[i])]=2;
b[c++]=data[i];
}
}
else if(flag[(data[i])]==2)
{
b[c++]=data[i];
}
}
if(c==(data.length))
{
return b;
}
if(c==0)
{
int ne[]=new int[0];
return ne;
}
else
{
int ne[]=new int[c];
for(int k=0;k<c;k++)
{
ne[k]=b[k];
}
return ne;
}
}
It passed 8 test cases but is failing for the last test case and I am not able to figure out what could the test case since that one is a hidden case. Any idea?