I am stuck in the following program:
I have an input integer array which has only one non duplicate number, say {1,1,3,2,3}. The output should show the non duplicate element i.e. 2.
So far I did the following:
public class Solution {public int singleNumber(int[] arr){int size = arr.length;int temp = 0;int result = 0;boolean flag = true;int[] arr1 = new int[size];for(int i=0;i<size;i++){temp = arr[i];for(int j=0;j<size;j++){if(temp == arr[j]){if(i != j)//System.out.println("Match found for "+temp);flag = false;break;}}}return result;}public static void main(String[] args) {int[] a = {1,1,3,2,3};Solution sol = new Solution();System.out.println("SINGLE NUMBER : "+sol.singleNumber(a));}}
Restricting the solution in array is preferable. Avoid using collections,maps.
Best Answer
public class NonRepeatingElement {public static void main(String[] args) {int result =0;int []arr={3,4,5,3,4,5,6};for(int i:arr){result ^=i;}System.out.println("Result is "+result);}}
Since this is almost certainly a learning exercise, and because you are very close to completing it right, here are the things that you need to change to make it work:
- Move the declaration of
flag
inside the outer loop - the flag needs to be set totrue
every iteration of the outer loop, and it is not used anywhere outside the outer loop. - Check the
flag
when the inner loop completes - if theflag
remainstrue
, you have found a unique number; return it.
From Above here is the none duplicated example in Apple swift 2.0func noneDuplicated(){ let arr = [1,4,3,7,3] let size = arr.count var temp = 0 for i in 0..<size{ var flag = true temp = arr[i] for j in 0..<size{ if(temp == arr[j]){ if(i != j){ flag = false break } } } if(flag == true){ print(temp + " ,") } } }// output : 1 , 4 ,7// this will print each none duplicated
/// for duplicate array static void duplicateItem(int[] a){/*You can sort the array before you compare*/int temp =0;for(int i=0; i<a.length;i++){for(int j=0; j<a.length;j++){if(a[i]<a[j]){temp = a[i];a[i] = a[j];a[j] = temp;}}}int count=0;for(int j=0;j<a.length;j++) {for(int k =j+1;k<a.length;k++) {if(a[j] == a[k]) {count++;}}if(count==1){System.out.println(a[j]);}count = 0;}}/* for array of non duplicate elements in array just change int k=j+1; to int k = 0; in for loop*/static void NonDuplicateItem(int[] a){/*You can sort the array before you compare*/int temp =0;for(int i=0; i<a.length;i++){for(int j=0; j<a.length;j++){if(a[i]<a[j]){temp = a[i];a[i] = a[j];a[j] = temp;}}}int count=0;for(int j=0;j<a.length;j++) {for(int k =0 ;k<a.length;k++) {if(a[j] == a[k]) {count++;}}if(count==1){System.out.println(a[j]);}count = 0;}}public class DuplicateItem {public static void main (String []args){int[] a = {1,1,1,2,2,3,6,5,3,6,7,8};duplicateItem(a);NonDuplicateItem(a);}
/// for first non repeating element in array ///static void FirstNonDuplicateItem(int[] a){/*You can sort the array before you compare*/int temp =0;for(int i=0; i<a.length;i++){for(int j=0; j<a.length;j++){if(a[i]<a[j]){temp = a[i];a[i] = a[j];a[j] = temp;}}}int count=0;for(int j=0;j<a.length;j++) {//int k;for(int k =0; k<a.length;k++) {if(a[j] == a[k]) {count++;}}if(count==1){System.out.println(a[j]);break;}count = 0;}}public class NonDuplicateItem {public static void main (String []args){int[] a = {1,1,1,2,2,3,6,5,3,6,7,8};FirstNonDuplicateItem(a);}
I have a unique answer, it basically takes the current number that you have in the outer for loop for the array and times it by itself (basically the number to the power of 2). Then it goes through and every time it sees the number isn't equal to double itself test if its at the end of the array for the inner for loop, it is then a unique number, where as if it ever find a number equal to itself it then skips to the end of the inner for loop since we already know after one the number is not unique.
public class Solution {public int singleNumber(int[] arr){int size = arr.length;int temp = 0;int result = 0;int temp2 = 0;int temp3 = 0;boolean flag = true;int[] arr1 = new int[size];for(int i=0;i<size;i++){temp = arr[i];temp2 = temp*temp;for(int j=0;j<size;j++){temp3 = temp*arr[j];if(temp2==temp3 && i!=j)j=arr.lengthif(temp2 != temp3 && j==arr.length){//System.out.println("Match found for "+temp);flag = false;result = temp;break;}}}return result;}public static void main(String[] args) {int[] a = {1,1,3,2,3};Solution sol = new Solution();System.out.println("SINGLE NUMBER : "+sol.singleNumber(a));}}
not tested but should work
public class Solution {public int singleNumber(int[] arr){int size = arr.length;int temp = 0;int result = 0;boolean flag = true;int[] arr1 = new int[size];for(int i=0;i<size;i++){temp = arr[i];int count=0;for(int j=0;j<size;j++){if(temp == arr[j]){count++;}}if (count==1){result=temp;break;}}return result;}
Try:
public class Answer{public static void main(String[] args) {int[] a = {1,1,3,2,3};int[] b =new int[a.length]; //instead of a.length initialize it to maximum element value in a; to avoid//ArrayIndexOutOfBoundsExceptionfor(int i=0;i<a.length;i++){int x=a[i];b[x]++;}for(int i=0;i<b.length;i++){if(b[i]==1){System.out.println(i); // outputs 2break;}}}}
PS: I'm really new to java
i usually code in C
.
Thanks @dasblinkenlight...followed your method
public class Solution {public int singleNumber(int[] arr){int size = arr.length;int temp = 0;int result = 0;int[] arr1 = new int[size];for(int i=0;i<size;i++){boolean flag = true;temp = arr[i];for(int j=0;j<size;j++){if(temp == arr[j]){if(i != j){// System.out.println("Match found for "+temp);flag = false;break;}}}if(flag == true)result = temp;}return result;}public static void main(String[] args) {int[] a = {1,1,3,2,3};Solution sol = new Solution();System.out.println("SINGLE NUMBER : "+sol.singleNumber(a));}}
One disastrous mistake was not enclosing the content of if(i != j)
inside braces. Thanks all for your answers.
If you are coding for learning then you can solve it with still more efficiently.
- Sort the given array using merge sort of Quick Sort.Running time will be nlogn.
- The idea is to use Binary Search.Till required element found All elements have first occurrence at even index (0, 2, ..) and next occurrence at odd index (1, 3, …).After the required element have first occurrence at odd index and next occurrence at even index.
Using above observation you can solve :
a) Find the middle index, say ‘mid’.
b) If ‘mid’ is even, then compare arr[mid] and arr[mid + 1]. If both are same, then the required element after ‘mid’ else before mid.
c) If ‘mid’ is odd, then compare arr[mid] and arr[mid – 1]. If both are same, then the required element after ‘mid’ else before mid.
Another simple way to do so..
public static void main(String[] art) {int a[] = { 11, 2, 3, 1,1, 6, 2, 5, 8, 3, 2, 11, 8, 4, 6 ,5};Arrays.sort(a);System.out.println(Arrays.toString(a));for (int j = 0; j < a.length; j++) {if(j==0) {if(a[j]!=a[j+1]) {System.out.println("The unique number is :"+a[j]);}}elseif(j==a.length-1) {if(a[j]!=a[j-1]) {System.out.println("The unique number is :"+a[j]);}}elseif(a[j]!=a[j+1] && a[j]!=a[j-1]) {System.out.println("The unique number is :"+a[j]);}}}
Happy Coding..
Using multiple loops the time complexity is O(n^2), So the effective way to resolve this using HashMap which in the time complexity of O(n). Please find my answer below,
`public static int nonRepeatedNumber(int[] A) {
Map<Integer, Integer> countMap = new HashMap<>();int result = -1;for (int i : A) {if (!countMap.containsKey(i)) {countMap.put(i, 1);} else {countMap.put(i, countMap.get(i) + 1);}}Optional<Entry<Integer, Integer>> optionalEntry = countMap.entrySet().stream().filter(e -> e.getValue() == 1).findFirst();return optionalEntry.isPresent() ? optionalEntry.get().getKey() : -1;
}}`