How to Find Unique Number That Is Not Repeated Twice

In this example, we’ll find a unique number that is not repeated twice:

Input:
int[] arr1 = { 2, 2, 5, 7, 7, 1, 1 };

Output:
5

1. Java

public class FindUniqueNumberThatIsNotRepeatedTwice {

	public static void main(String[] args) {
		int unSortedArray[] = { 1, 1, 3, 3, 2, 5, 5, 6, 6 };
		findNumber(unSortedArray);
	}

	private static void findNumber(int arr[]) {
		List<Integer> elements = new ArrayList<>();
		for (int i : arr) {
			if (!elements.contains(i)) {
				elements.add(i);
			} else {
				elements.remove(Integer.valueOf(i));
			}
		}
		System.out.println(elements.get(0));
	}

}

Output:

2

Complete Java Solutions can be found here: Github Link

The complete list can be found here: Practise Programming Questions

Leave a Comment