Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

findng and replacing duplicate values in an array

int[] arr = { 123, 129, 1928, 918273645, 12345, 123, 543, 543, 123, 123 };
int size = arr.length;
		
Scanner in = new Scanner(System.in);

for (int i = 0; i < size; i++) {
	for (int j = i; j < size; j++) {
		if (i == j) {
			continue;
		}
		if (arr[i] == arr[j]) {
			System.out.println("Duplicate found of " + i + " at " + j);
			System.out.print("Input replacement: ");
			int replacement = in.nextInt();
			boolean duplicate = true;
            do {
				try {
					arr[j] = replacement;
					for (int k = 0; k < j; k++) {
						if (arr[j] == arr[k]) {
							System.out.println("You just input at " + j + " the same value as  " + k);
							throw new Exception();
						}
					}
					duplicate = false;
				} catch (Exception e) {
					System.out.println("Please enter another value: ");
					replacement = in.nextInt();
				}
			} while (duplicate);
		}
	}
}
Comment

PREVIOUS NEXT
Code Example
Java :: x squared unicode java code 
Java :: Magic square java user input 
Java :: scanner in = new scanner(system.in) meaning in java 
Java :: java.lang.noclassdeffounderror even though class is present 
Java :: A method that returns a stream of JUnit Arguments 
Java :: jsf prefix tag 
Java :: java blocks 
Java :: difido 
Java :: compile option in android 
Java :: how to use old android studio project 
Java :: java find nth largest element using priority queue heap 
Java :: react-native maven package 404 not found 
Java :: Original authors: Talmon Marco, Igor Magazinnik Programming languages: Java, C, Python, C , Qt, Objective-C 
Java :: Java 8 merge multiple collections. 
Java :: aspectj after returning 
Java :: has places api got deprecated??? 
Java :: two array structures in java 
Java :: java.sql.SQLException: Invalid column index 
Java :: how to create gravity in Java 
Java :: franchiseRulesTemp 
Java :: java print color in console eclipse 
Java :: java to kotlin online convertor 
Java :: informaticapc 
Java :: gggghfh 
Java :: integer to roman 
Java :: show all debug points intellij 
Java :: boolean parse jtextfield 
Java :: on offline event spigot example 
Java :: android java onUpgrade() 
Java :: java union of sets 
ADD CONTENT
Topic
Content
Source link
Name
7+1 =