Java 在数组中查找非重复元素
我被困在以下程序中: 我有一个输入整数数组,它只有一个非重复数,比如{1,1,3,2,3}。输出应显示非重复元素,即2 到目前为止,我做了以下工作:Java 在数组中查找非重复元素,java,arrays,Java,Arrays,我被困在以下程序中: 我有一个输入整数数组,它只有一个非重复数,比如{1,1,3,2,3}。输出应显示非重复元素,即2 到目前为止,我做了以下工作: public class Solution { public int singleNumber(int[] arr){ int size = arr.length; int temp = 0; int result = 0; boolean flag = true;
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));
}
}
公共类解决方案{
公共整数单号(整数[]arr){
int size=arr.length;
内部温度=0;
int结果=0;
布尔标志=真;
int[]arr1=新的int[size];
对于(int i=0;i,因为这几乎肯定是一个学习练习,而且因为您已经非常接近正确地完成它了,所以以下是您需要更改以使其工作的事项:
- 在外循环内部移动
标志的声明
-该标志需要在外循环的每次迭代中设置为true
,并且不在外循环之外的任何地方使用
- 当内部循环完成时,检查
标志
-如果标志
保持为真
,则您已找到一个唯一的数字;返回它
未经测试,但应能正常工作
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;
}
公共类解决方案{
公共整数单号(整数[]arr){
int size=arr.length;
内部温度=0;
int结果=0;
布尔标志=真;
int[]arr1=新的int[size];
对于(int i=0;i尝试:
公共类答案{
公共静态void main(字符串[]args){
int[]a={1,1,3,2,3};
int[]b=新的int[a.长度];
//而不是a.length将其初始化为a;中的最大元素值以避免
//ArrayIndexOutOfBoundsException
对于(int i=0;i我有一个唯一的答案,它基本上取数组外部for循环中的当前数字,并将其自身乘以(基本上是2的幂)。然后它通过,每当它看到数字不等于自身的两倍时,测试它是否位于内部for循环的数组末尾,它就是一个唯一的数字,如果它曾经找到一个等于自身的数字,它就会跳到内部for循环的末尾,因为我们已经知道在1之后该数字不是唯一的
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.length
if(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));
}
}
公共类解决方案{
公共整数单号(整数[]arr){
int size=arr.length;
内部温度=0;
int结果=0;
int temp2=0;
int temp3=0;
布尔标志=真;
int[]arr1=新的int[size];
对于(int i=0;i谢谢@dasblinkenlight…遵循您的方法
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));
}
}
公共类解决方案{
公共整数单号(整数[]arr){
int size=arr.length;
内部温度=0;
int结果=0;
int[]arr1=新的int[size];
对于(int i=0;i,这里是Apple swift 2.0中的无重复示例
func非重复()
设arr=[1,4,3,7,3]
let size=arr.count
变量温度=0
对于0中的i..如果您是为了学习而编码,那么您可以更有效地解决它
使用快速排序的合并排序对给定数组进行排序。
运行时间将为nlogn
这个想法是使用二进制搜索。
直到找到所需元素为止,所有元素第一次出现在偶数索引(0,2,…),下一次出现在奇数索引(1,3,…)。
在所需元素第一次出现在奇数索引中,下一次出现在偶数索引中之后
使用上述观察结果,您可以解决以下问题:
a) 找到中间的索引,说“mid”
b) 如果“mid”是偶数,则比较arr[mid]和arr[mid+1]。如果两者相同,则“mid”之后的必需元素在mid之前
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);
}
}
c) 如果“mid”是奇数,则比较arr[mid]和arr[mid–1]。如果两者相同,则“mid”之后的必需元素在mid之前。对于重复数组
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);
}
}
静态无效重复项(int[]a){
/*
您可以在比较之前对数组进行排序
*/
内部温度=0;
for(int i=0;i///用于数组中的第一个非重复元素///
静态void FirstNonDuplicateItem(int[]a){
/*
您可以在比较之前对数组进行排序
*/
内部温度=0;
对于(inti=0;i另一种简单的方法
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]);
}
}else
if(j==a.length-1) {
if(a[j]!=a[j-1]) {
System.out.println("The unique number is :"+a[j]);
}
}else
if(a[j]!=a[j+1] && a[j]!=a[j-1]) {
System.out.println("The unique number is :"+a[j]);
}
}
}
publicstaticvoidmain(字符串[]艺术){
inta[]={11,2,3,1,1,6,2,5,8,3,2,11,8,4,6,5};
数组。排序(a);
System.out.println(Arrays.toString(a));
对于(int j=0;j
快乐编码..使用多个循环时间复杂度为O(n^2),因此使用HashMap(时间复杂度为O(n))解决此问题的有效方法。请在下面找到我的答案
`公共静态int非重复编号(int[]A){
Map countMap=newhashmap();
int结果=-1;
对于(int i:A){
如果(!countMap.containsKey(i)){
countMap.put(i,1);
}否则{
countMap.put(i,countMap.get(i)+1);
}
}
可选optionalEntry=countMap.entrySet().stream()
.filter(e->e.getValue()==1.findFirst();
返回optionalEntry.isPresent()?optionalEntry.get().getKey():-1;
}
}`可爱的基于XOR的解决方案可以工作…你能使用另一个数组吗?如果可以的话,我有一个解决方案。数组是否只包含一个唯一的数字?@CoolGuy没有,但我想我找到了另一个解决方案,将数字的幂或2,然后取原始数字乘以每个数字,如果它等于e数取二的幂,如3^2=9
/// 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);
}
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]);
}
}else
if(j==a.length-1) {
if(a[j]!=a[j-1]) {
System.out.println("The unique number is :"+a[j]);
}
}else
if(a[j]!=a[j+1] && a[j]!=a[j-1]) {
System.out.println("The unique number is :"+a[j]);
}
}
}
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;