+ 1

Java array Problem

Hi, I had written a code which removes repeated number in an array. I asked for help previously but someone gave me an advice to look for documentation but I am not smart enough to find out what I am missing in my code or knowledge. Please correct my code and my pseudo knowledge. It will be a great help. https://code.sololearn.com/cobSJv381yDz/?ref=app

13th Aug 2021, 1:31 PM
Nitin Tiwari
Nitin Tiwari - avatar
6 Answers
+ 5
It seems a bit too complicated than it should. Step 1: Declare a variable that will count how many unique variables there are in the array. Step 2: Create an array with the size of the variable that you took care of. Step 3 (not that efficient): Loop through the original array, then loop through the new array and check if the element from the outer array exists within the new array already... int lastIndex = 0; for (int i = 0; i < original.length; i++){ int currentElement = original[i]; boolean exists = false; for (int j = 0; j < newArr.length; j++){ if (newArr[j] == currentElement){ exists = true; } } if (!exists){ newArr[lastIndex] = currentElement; lastIndex++; } }
13th Aug 2021, 2:21 PM
Yahel
Yahel - avatar
+ 2
Martin Taylor thanks for your help but Yahel said right I just wanted to use arrays to grow logically, I know that there are many easy ways one can use, but using it in learning period is not at all suggested.
13th Aug 2021, 8:43 PM
Nitin Tiwari
Nitin Tiwari - avatar
+ 1
Martin Taylor Great solution, but I think he wanted to use simple concepts in order to achieve his goal...
13th Aug 2021, 5:05 PM
Yahel
Yahel - avatar