java - Identifying repeating numbers in a array -
java - Identifying repeating numbers in a array -
i dealing next problem. not looking provide me solution looking guidance solving problem. here have come far.
i have tried first set ( around values repeat. getting out of bounds error. appreciate if can force me towards right path coding little algorithm handle problem.
my code (in progress)
import java.util.random; public class test { public static void main(string[] args) { int[] values = { 1, 2, 5, 5, 3, 1, 2, 4, 3, 2, 2, 2, 2, 3, 6, 5, 5, 6, 3, 1 }; boolean inrun = false; (int = 0; < values.length; i++) { if (values[i] == values[i + 1] && values[i + 1] < values.length) { system.out.print("("); } system.out.print(values[i]); } } }
you need iterate array , if found pair iterate 1 time again in while loop until find non pair.
sample:
int[] values = { 1, 2, 5, 5, 3, 1, 2, 4, 3, 2, 2, 2, 2, 3, 6, 5, 5, 6, 3, 1 }; boolean inrun = false; (int = 0; < values.length; i++) { if (i + 1 < values.length && values[i] == values[i + 1] ) { system.out.print("("); while (i + 1 < values.length && values[i] == values[i + 1] ) { system.out.print(values[i++]); } system.out.print(values[i++]); system.out.print(")"); } system.out.print(values[i]); }
result:
12(55)31243(2222)36(55)631
java algorithm
Comments
Post a Comment