Java Arrays: Finding Unique Numbers In A Group of 10 Inputted Numbers RE: -
java arrays: finding unique numbers in grouping of 10 inputted numbers
i have problem have looked doestovsky's question question, need know on how create part on finding duplicates function of it's own:
java.util.scanner input = new java.util.scanner(system.in); int[] numbers = new int[10]; boolean[] usedbefore = new boolean[10]; // insert numbers (int = 0; < numbers.length; i++) { // read number console numbers[i] = input.nextint(); // check if number inserted before usedbefore[i] = false; for(int k = 0; k < i; k++) { if(numbers[k] == numbers[i]) { usedbefore[i] = true; break; } } } // print numbers not inserted before for(int j = 0; j < numbers.length; j++) { if(!usedbefore[i]) { system.out.print(string.valueof(numbers[j])+" "); } }
i have tried part of code , worked need take part find duplicates function of it's own powered arrays.
credits thimokl creating code.
let's seek else, using treeset, , let's rid of for
import java.util.*; public class duplicate { private static treeset<integer> numbers = new treeset<integer>(); private static treeset<integer> duplicates = new treeset<integer>(); public static void main (string[] args) { scanner input = new scanner(system.in); int n=0; int numberofinttoread=10; (int = 0; < numberofinttoread; i++) { // read number console n=input.nextint(); // check if number inserted before checkifduplicate(n); } // print numbers not inserted more 1 time (integer j : numbers) { system.out.print(j+" "); } } public static void checkifduplicate(int n){ if(!numbers.contains(n) && !duplicates.contains(n)){ numbers.add(n); }else{ numbers.remove(n); duplicates.add(n); } } }
but if want utilize arrays not collection of sort, need declare arrays class members, way can set "for" checks duplicates in function give i, , way can set "for" printing in function. , give numbers.length. should trick.
java arrays int
No comments:
Post a Comment