How to remove redundant array values ..? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to remove redundant array values ..?

I have an array arr=[1,2,3,3,4,4,5,5,6] i need to remove the duplicates values of the array arr. how ?

24th Dec 2018, 2:52 PM
Techie Praga
Techie Praga - avatar
6 Answers
+ 7
Techie Praga, this is a place to ask questions and answer other people's questions. It isn't meant to answer your own questions and give yourself the best answer. By the way, your code is incomplete. https://www.sololearn.com/Discuss/1316935/welcome-to-sololearn-forum/ You can advertise your codes here: https://www.sololearn.com/Discuss/452626/off-topic-code-advertising
24th Dec 2018, 3:22 PM
Anna
Anna - avatar
+ 5
You could also try to transfer the values of the array to a set. The set will only store the unique values of the array.
24th Dec 2018, 4:38 PM
Lambda_Driver
Lambda_Driver - avatar
+ 3
My proposition 😊 // short and simple solution https://code.sololearn.com/c880WSnCyPFz/?ref=app
24th Dec 2018, 5:52 PM
Michal
Michal - avatar
+ 1
// simple java program to remove duplicates array values. // techie praga codes class Main { // Function // This function- removeDuplicates (to remove duplicate elements)returns new size of modified array static int removeDuplicates(int arr[], int n) { if (n == 0 || n == 1) return n; // To store index of next unique element int j = 0; // Doing same as done in Method 1 // Just maintaining another updated index i.e. j for (int i = 0; i < n-1; i++) if (arr[i] != arr[i+1]) arr[j++] = arr[i]; arr[j++] = arr[n-1]; return j; } public static void main (String[] args) { int arr[] = {1, 2, 2, 3, 4, 4, 4, 5, 5}; int n = arr.length; n = removeDuplicates(arr, n); // Print updated array for (int i=0; i<n; i++) System.out.prin
24th Dec 2018, 2:55 PM
Techie Praga
Techie Praga - avatar
0
sir could you pls tell me how to learn *all* programming langs without practicing in 1day😀
24th Dec 2018, 3:00 PM
Mansoor Ahmed
Mansoor Ahmed - avatar
0
tnx for ur suggestions, btw (we solve our some prgms here)and share the link to our frnds who are willing to learn java
24th Dec 2018, 3:33 PM
Mansoor Ahmed
Mansoor Ahmed - avatar