Skip to content

Instantly share code, notes, and snippets.

@appikonda
Created April 30, 2020 21:02
Show Gist options
  • Save appikonda/7d765de68aa6cf87d56000e0c929c3fa to your computer and use it in GitHub Desktop.
Save appikonda/7d765de68aa6cf87d56000e0c929c3fa to your computer and use it in GitHub Desktop.
import java.util.ArrayList;
import java.util.List;
public class InterviewQuestion {
public static void main(String[] args) {
int arr[] = { 2, 2, 3, 4 };
int num = 4;
System.out.println("Number is: " + num + "and its count :" + countNum(arr, num));
}
static int countNum(int[] arr, int num) {
List<Integer> al = new ArrayList<>(arr.length);
for (int i : arr) {
al.add(Integer.valueOf(i));
}
int i = al.indexOf(num);
int count = 0;
int nextIndex = 0;
while (i < arr.length) {
nextIndex = al.indexOf(num + 1);
if (nextIndex != -1) {
count++;
num++;
} else {
break;
}
i++;
}
return count;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment