Find All Numbers Disappeared in an Array (Getting IndexOutOFBoundsException - java

I keep getting a IndexOutOFBoundsException and i cant seem to find where. Ive spent ages trying to figure this out. Any help would be appreciated. Thanks
class Solution {
public List<Integer> findDisappearedNumbers(int[] nums) {
if(nums.length < 2){
return new ArrayList<Integer>();
}
List<Integer> ret = new ArrayList<Integer>(nums.length);
for(int i=0; i < nums.length; i++){
ret.set(i,i);
}
for(int i=0; i < nums.length; i++){
ret.set(nums[i]-1, nums[i]);
}
for(int i=0; i < nums.length; i++){
if(ret.get(i) == 0){
ret.set(i, i+1);
}
else{
ret.remove(i);
}
}
return ret;
}
}

for(int i=0; i < nums.length; i++){
ret.set(nums[i]-1, nums[i]);
}
There you set the nums[i]-1s index to nums[i] but it will throw the error if nums[i] is either smaller or equal to 0 or higher than the length.
As the other answer from #Bahij.Mik states(make sure to upvote it), there are no elements in the list at this point.
You may want to use add() instead of set() here.
Note that new ArrayList<>(number) will create a new ArrayList with an initial space for n elements, it will not fill the elements like it would happen with an array(not even with null)
Also, in
for(int i=0; i < nums.length; i++){
if(ret.get(i) == 0){
ret.set(i, i+1);
}
else{
ret.remove(i);
}
}
you are removing elements while iterating over the ArrayList.
This will reduce the size of the ArrayList and you will iterate out of the bounds if you removed an element at some point.

There are several issues in your code, first thing that comes to sight is that when you are instantiating your arraylist with
List<Integer> ret = new ArrayList<Integer>(nums.length);
You are just setting the list's initial capacity not the size, so you can't set elements at specific indices, instead you need to add elements to the list then you can modify them at said indices, so instead of the ret.set(i, i) use ret.add(i).

Here is a solution
class Solution {
public List<Integer> findDisappearedNumbers(int[] nums) {
List<Integer> ret = new ArrayList<>();
for(int i = 0; i < nums.length; i++){
int index = Math.abs(nums[i]) - 1;
if(nums[index] > 0 ){
nums[index] *= -1;
}
}
for(int j = 1; j <= nums.length; j++){
if(nums[j-1] > 0 ){
ret.add(j);
}
}
return ret;
}
}

Related

I was attempting the 'Contains Duplicate' problem on Leetcode. My code seems fine but I am receiving the Time limit exceeded error

Is there a better way to solve this problem through nested loops, or Hashtable (or any other methods)are the only way to deal with this error?
class Solution {
public boolean containsDuplicate(int[] nums) {
// traversing the array to find an element equal to the chosen element
for (int i = 0; i < nums.length; i++){
// now after one element is chosen traversing the rest to find its equal
for (int j = i+1; j < nums.length; j++){
int dupli = nums[i];
if (dupli == nums [j]){
return true;
}
}
}
return false;
}
}

Why does my Arraylist keep printing [3,2]?

I'm trying to remove every instance of value from an array of integers, and return the length of the new array of integers.The input is [3,2,2,3], with val being 3. The output should be [2,2], with length 2. I keep getting [3,2], but I am removing val through an array.
class Solution {
public int removeElement(int[] nums, int val) {
ArrayList<Integer> list = new ArrayList<Integer>();
for(int i = 0; i < nums.length; i++) {
list.add(nums[i]);
}
if(list.isEmpty()) {
return 0;
}
for(int i = 0; i < list.size(); i++) {
if(list.get(i) == val) {
list.remove(i);
}
}
return list.size();
}
}
When you remove the first 3 you will have i=0 and next the array list will resize, so when you delete you need to do i - -.
class Solution {
public int removeElement(int[] nums, int val) {
ArrayList<Integer> list = new ArrayList<Integer>();
for(int i = 0; i < nums.length; i++) {
list.add(nums[i]);
}
if(list.isEmpty()) {
return 0;
}
for(int i = 0; i < list.size(); i++) {
if(list.get(i) == val) {
list.remove(i);
i--;
}
}
return list.size();
}
}
You haven’t shown any printing code but I assume you print nums based on the number returned by this method. That means you never change nums in any way and just print the first n numbers from there. You need to return the modified array instead of just the length.
You do not need to call remove at all (while removing like you do, it is okay, but for example, if you do this in a foreach cycle, it would crash on ConcurrentModificationException. Instead, you can determine whether to add item to the resulting list or not inside the first for loop (which is in my opinion safer):
ArrayList<Integer> list = new ArrayList<Integer>();
for(int i = 0; i < nums.length; i++) {
if (nums[i] != val) {
list.add(nums[i]);
}
}
return list.size();
(I know that this does not answer your question directly)

How to return only needed values without 0's and nulls in this question?

I have this question that I have been tackling for a while.
"The method should return an array containing the elements that are divisible by a certain number" in this case the target which is 5.
Here is my solution
public static int[] Divisible(int[] array, int target){
int[] answer = new int[array.length];
for (int i = 0; i<array.length; i++){
if (array[i] % target == 0){
answer[i] = array[i];
}
}
return answer;
}
assuming my input is
int[] input = {5,3,6,10};
my output will be [5,0,0,10].
My desired output should be [5,10].
please, How do I get rid of the zeros
The basic idea is to fill the answer array from the bottom, and then truncate it to exactly the size you need.
int j = 0;
for (int i=0; i<array.length; i++) {
if (array[i] % target == 0){
answer[j++] = array[i];
}
}
return Arrays.copyOf(answer, j);
Arrays is a standard Java utility class.
If you're not allowed to use the Arrays utility class then the last line can be replaced by:
int[] answer2 = new int[j];
for (int i=0; i<j; i++)
answer2[i] = answer[i];
return answer2;
This feels a little clunky to me but it satisfies the apparent requirements to use simple arrays.
you can do like this
public static int[] Divisible(int[] array, int target){
List<Integer> list = new ArrayList<>();
for (int i = 0; i<array.length; i++){
if (array[i] % target == 0){
list.add(array[i]);
}
}
int[] ints = new int[list.size()];
for (int i = 0; i < ints.length; i++) {
ints[i] = list.get(i);
}
return ints;
}
Two options:
you can use an ArrayList internally, as that can grow dynamically (you just keep adding the values you are interested in, done). If you have to return an array, you can easily do that based on your filled list.
when going only with arrays you can simply do 2 passes: first create that large array and fill it. Then count the non zero entries! Create a new array with the smaller length, and then copy over all non zero elements manually.
I have used the method stated by #GhostCat but it made me change the code entirely. This is my new code
public static Object[] Divisi(int[] array, int target){
ArrayList<Integer> answer = new ArrayList<>();
for (int i = 0; i<array.length; i++){
if (array[i] % target == 0){
answer.add(array[i]);
}
}
return answer.toArray();
}
This gave me the desired answer but how else can i do this without converting to object
The shortest and the most quickest way to go about this is, you can change the array to an ArrayList and then use .toArray() method to get it back as a primitive array when returning.
public static Integer[] Divisible(int[] array, int target){
List<Integer> answer = new ArrayList<>();
for (int i = 0; i<array.length; i++){
if (array[i] % target == 0) {
answer.add(array[i]); // pushing to list only if the number is divisible
}
}
return answer.toArray(new Integer[0]); // converting the list to an array before returning
}
If you want a list of unique numbers use Set instead of a List.
Set<Integer> answer = new HashSet<>();
Instead of using the primitive int, I have conformed to using Integer instead.
Here is my solution.
public static Integer[] divisible(Integer[] array, int target) {
int j = 0;
ArrayList<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < array.length; i++) {
if (array[i] % target == 0) {
j++;
list.add(array[i]);
}
}
Integer answer[] = new Integer[j];
answer = list.toArray(answer);
return answer;
}
You should use a separate counter for the result array and keep incrementing the counter if the number is divisible by the target.
Something like this :
int[] result=new int[array.length];
int resultindex=0; //create a separate counter
for(int i=0;i<array.length;i++)
{
if(array[i]%target==0)
{
result[resultindex]=array[i];
++resultindex; //update the counter
}
}

Find number of duplicate that occurs in array - Java

I can't wrap my head around this. Need to find duplicates and I did. All now that is left is to print how many times a duplicate appears in the array. I just started with Java,so this needs to be hard coded for me to understand. Spend last two days trying to figure it out but with no luck.. Any help will be great! Talk is cheap,here is the code..
import java.util.Arrays;
public class LoopTest {
public static void main(String[] args) {
int[] array = {12,23,-22,0,43,545,-4,-55,43,12,0,-999,-87};
int positive_counter = 0;
int negative_counter = 0;
for (int i = 0; i < array.length; i++) {
if(array[i] > 0) {
positive_counter++;
} else if(array[i] < 0) {
negative_counter++;
}
}
int[] positive_array = new int[positive_counter];
int[] negative_array = new int[negative_counter];
positive_counter = 0;
negative_counter = 0;
for (int i = 0; i < array.length; i++) {
if(array[i] > 0) {
positive_array[positive_counter++] = array[i];
} else if(array[i] < 0) {
negative_array[negative_counter++] = array[i];
}
}
System.out.println("Positive array: " + (Arrays.toString(positive_array)));
System.out.println("Negative array: " + (Arrays.toString(negative_array)));
Arrays.sort(array);
System.out.println("Array duplicates: ");
for (int i = 0; i < array.length; i++) {
for (int j = i + 1; j < array.length; j++) {
if(array[i] == array[j]) {
System.out.println(array[j]);
}
}
}
}
}
Since you are already sorting the array you can find the duplicates with just one loop (they will be next to each other right?). So you can do something like:
Arrays.sort(array);
System.out.println("Array duplicates: ");
int lastValueCount=1; //How many times we met the current value (at least 1 - this time)
for (int i = 1; i < array.length; i++){
if(array[i] == array[i-1])
lastValueCount++; //If it is the same as the previous increase the count
else {
if(lastValueCount>1) //If it is duplicate print it
System.out.println(array[i-1]+" was found "+lastValueCount+" times");
lastValueCount=1; //reset the counter
}
}
Result for your array is:
Array duplicates:
0 was found 2 times
12 was found 2 times
43 was found 2 times
Also you can use some of the Java bells and whistles like inserting the values into Map or something like that but I guess you are looking from an algorithmic point of view so the above is the simple answer with just one loop
Just go through your solution, first you separate positive and negative numbers in two different arrays, then you never use them, so what's the purpose of this separation ?
I am giving you just an idea related to your problem, it's better to solve it by your self so that you can get hands on Java.
Solution: you can use Dictionary-key value pair. Go through your array, put element in dictionary as a key and value as zero, on every iteration check if that key already exist in Dictionary, just increment its value. In the end, all of the values are duplicates that occurs in your array.
Hope it helps you.
From the algorithmic point of view, Veselin Davidov's answer is good (the most efficient).
In a production code, you would rather write it like this :
Map<Integer, Long> result =
Arrays.stream(array)
.boxed() //converts IntStream to Stream<Int>
.collect(Collectors.groupingBy(i -> i, Collectors.counting()));
The result is this Map :
System.out.println(result);
{0=2, 545=1, -4=1, -22=1, -87=1, -999=1, -55=1, 23=1, 43=2, 12=2}
An easy way would be using Maps. Without changing code too much:
for (int i = 0; i < array.length; i++) {
int count = 0;
for (int j = i + 1; j < array.length; j++) {
if(array[i] == array[j]) {
System.out.println(array[j]);
count++;
}
}
map.put(array[i], count);
}
Docs:
https://docs.oracle.com/javase/7/docs/api/java/util/Map.html
Edit: As a recommendation, after you are done with the example, you should analize your code and find what isn´t neccesary, what could be done better, etc.
Are all your auxiliary arrays neccesary? Are all loops necessary?
You can do it by creating an array list for duplicate values:-
Arrays.sort(array);
System.out.println("Array duplicates: ");
ArrayList<Integer> duplicates = new ArrayList<Integer>();
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length; j++) {
if(j != i && array[i] == array[j] && !duplicates.contains(array[i])){
duplicates.add(array[i]);
System.Out.println(duplicates[duplicates.size()-1]);
}
}
}
public static void findDuplicate(String s){
char[] charArray=s.toCharArray();
ArrayList<Character> duplicateList = new ArrayList<>();
System.out.println(Arrays.toString(charArray));
for(int i=0 ; i<=charArray.length-1; i++){
if(duplicateList.contains(charArray[i]))
continue;
for(int j=0 ; j<=charArray.length-1; j++){
if(i==j)
continue;
if(charArray[i] == charArray[j]){
duplicateList.add(charArray[j]);
System.out.println("Dupliate at "+i+" and "+j);
}
}
}
}

No duplicates in an array [duplicate]

This question already has answers here:
How to get unique values from array
(13 answers)
Closed 8 years ago.
void RemoveDups(){
int f=0;
for(int i=1;i<nelems;i++){
if(arr[f]==arr[i]){
for(int k=i;k<nelems;k++)
{
arr[k]=arr[k+1];
}
nelems--;
}
if(i==(nelems+1)){
f++;
i=f+1; //increment again
}
}
}
This is the logic i have written to remove duplicate elements from an array ,but this is not working at all ?what changes i should make to make it work? or you people have better logic for doing the same considering time complexity.and i don't want to use built-in methods to achieve this.
int end = input.length;
for (int i = 0; i < end; i++) {
for (int j = i + 1; j < end; j++) {
if (input[i] == input[j]) {
int shiftLeft = j;
for (int k = j + 1; k < end; k++, shiftLeft++) {
input[shiftLeft] = input[k];
}
end--;
j--;
}
}
}
I think you can use Set Collection
copy all the values to an HashSet and then using Iterator access the Values
Set<Integer> hashset= new HashSet<Integer>();
You have two options, C# has the Distinct() Linq expression that will do this for you (Missed the Java tag), however if you need to remove items, have you thought about sorting the list first, then comparing the current item to the previous item and if they're the same, remove them. It would mean your diplicate detection is only ever running through the array once.
If you're worried about sort you could easily implement an efficient bubble sort or somthing to that effect
You never decrease i after You compared for examlpe arr[0] to arr[5], You never will test arr[1] == arr[2]
You need to start a new loop (i) after You've incremented f.
try
for(int f=0;f<nelems-1;f++)
{
for(int i=f+1;i<nelems;i++)
{
...
}
}
with this nested for loop you can compare every two element of the array.
a good start is to eliminate duplicate elements without shrinking the array which is done lastly:
public class run2 extends Thread {
public static void main(String[] args) {
int arr[] = { 1, 2, 2, 3, 5, 6, 5, 5, 6, 7 };
for (int i = 0; i < arr.length; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] == -1)
j++;
if (arr[i] == arr[j])
arr[j] = -1;
}
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + ",");
}
System.out.println();
for (int i = 0; i < arr.length; i++) {
if (arr[i] == -1) {
for (int j = i; j < arr.length; j++) {
if (arr[j] != -1) {
arr[i] = arr[j];
arr[j] = -1;
break;
}
}
}
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + ",");
}
}
}
Adapt this code :
public static int[] removeDuplicates(int[] numbersWithDuplicates) {
// Sorting array to bring duplicates together
Arrays.sort(numbersWithDuplicates);
int[] result = new int[numbersWithDuplicates.length];
int previous = numbersWithDuplicates[0];
result[0] = previous;
for (int i = 1; i < numbersWithDuplicates.length; i++) {
int ch = numbersWithDuplicates[i];
if (previous != ch) {
result[i] = ch;
}
previous = ch;
}
return result;
}
As far as I understood from your code,you are comparing each value starting from index 0 to the rest of the element and when you see the element which is located at index f your are trying to shift the entire array and decrementing the size of array(nelems).Look at line no. 11
if(i==(nelems+1)){
f++;
i=f+1;
The problem is when i is set to f+1,i will again be incremented in the for loop for the next iteration.So basically i starts comparing from f+2.And also you are comparing i with (nelems+1) considering the case when nelems decremented but you are not considering the case when i reaches the end without decreasing nelems in that case i will never be equale to (nelems+1).Now considering your logic you could do 2 things.
1.Here is your working code.
for(int i=1;i<nelems;i++){
if(arr[f]==arr[i]){
for(int k=i+1;k<nelems;k++)
{
arr[k-1]=arr[k];
}
if(i==(nelems-1)){
f++;
i=f;
}
nelems--;
}
if(i==(nelems-1)){//end of the loop
f++;
i=f; //increment again
}
}
2.You could use an outer for loop alternatively that will increment the f value once the inner for is completed.
void RemoveDups(){
for(int f=0;f<nelems;++f){
for(int i=1;i<nelems;i++){
if(arr[f]==arr[i]){
for(int k=i;k<nelems;k++)
arr[k]=arr[k+1];
nelems--;
}
}
}
}
Now your problem is solved but the time complexity of your code will be(O(N^3)).
Now instead of shifting the entire array at line 4,you could just swap the arr[f] with last element.
if(arr[f]==arr[i]){
swap(arr[f],arr[nelems-1]);
nelems--;
}
it will reduce the time complexity from O(N^3) to O(N^2).
Now I'll suggest you my method
1.just sort the array.It will be done in O(NlogN).
2.now using one for loop you can get what do you wanted.
void RemoveDups(){
int k=0,i;
for(i=1;i<nelems;++i){
while(arr[i]==arr[i-1])
++i;
arr[k++]=arr[i-1];
}
arr[k++]=arr[i-1];
}
Now basically you got an array of size k,which contains non repeated element in sorted order and the time complexity of my solution is O(NlogN).

Categories

Resources