Java 在数组中查找唯一整数
我有一个像这样的整数数组: A={1,1,4,4,4,1,1} 我想数一次每个数字,在这个例子中,awnser是2,因为我想数一次1和4 我不想使用排序方法 我无法找到一种方法来解决它使用java。 我这样做了,但它给了我0Java 在数组中查找唯一整数,java,arrays,logic,Java,Arrays,Logic,我有一个像这样的整数数组: A={1,1,4,4,4,1,1} 我想数一次每个数字,在这个例子中,awnser是2,因为我想数一次1和4 我不想使用排序方法 我无法找到一种方法来解决它使用java。 我这样做了,但它给了我0 public static void main(String args[]) { int a[] = { 1,1,4,4,4,4,1,1}; System.out.print(new Test4().uniques(a)); } public in
public static void main(String args[]) {
int a[] = { 1,1,4,4,4,4,1,1};
System.out.print(new Test4().uniques(a));
}
public int uniques(int[] a) {
int unique = 0;
int tempcount = 0;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a.length; j++) {
if (a[i] == a[j]) {
tempcount++;
}
}
if (tempcount <= 2) {
unique=a[i];
}
tempcount = 0;
}
return unique;
}
publicstaticvoidmain(字符串参数[]){
int a[]={1,1,4,4,4,4,1,1};
System.out.print(新Test4().uniques(a));
}
公共内部唯一性(内部[]a){
int unique=0;
int tempcount=0;
for(int i=0;i
int test[]={1,1,4,4,4,1,1};
Set<Integer> set=new LinkedHashSet<Integer>();
for(int i=0;i<test.length;i++){
set.add(test[i]);
}
System.out.println(set);
最后,集合将包含唯一的整数
请尝试以下代码:
int test[]={1,1,4,4,4,1,1};
Set<Integer> set=new LinkedHashSet<Integer>();
for(int i=0;i<test.length;i++){
set.add(test[i]);
}
System.out.println(set);
在结尾<代码> SET>代码>将包含唯一的<代码>整数 >
如果仅限于数组,请考虑尝试:
让我们取一个与原始数组大小相同的临时数组,其中存储每个唯一的字母,并假设a
是原始数组
int[] tempArray= new int[a.length];
int tempArraycounter = 0;
bool isUnique = true;
for (int i = 0; i < a.length; i++)
{
isUnique = true;
for (int j = 0; j < tempArray.length; j++)
{
if(tempArray[j] == a[i])
isUnique = false;
}
if(isUnique)
{
tempArray[tempArraycounter] = a[i];
tempArraycounter++;
isUnique = false;
}
}
int[]tempArray=newint[a.length];
int tempArraycounter=0;
bool isUnique=true;
for(int i=0;i
现在>代码> TEMPARRAYORT 将是您的答案;
< P>如果仅限于数组,请考虑尝试:
让我们取一个与原始数组大小相同的临时数组,其中存储每个唯一的字母,并假设a
是原始数组
int[] tempArray= new int[a.length];
int tempArraycounter = 0;
bool isUnique = true;
for (int i = 0; i < a.length; i++)
{
isUnique = true;
for (int j = 0; j < tempArray.length; j++)
{
if(tempArray[j] == a[i])
isUnique = false;
}
if(isUnique)
{
tempArray[tempArraycounter] = a[i];
tempArraycounter++;
isUnique = false;
}
}
int[]tempArray=newint[a.length];
int tempArraycounter=0;
bool isUnique=true;
for(int i=0;i
现在,tempArraycounter
将是您的答案;)好的,首先在您的解决方案中,您将返回int unique
,您将其设置为唯一的值a[i]
。因此在您的示例中,它只返回1或4
接下来,关于一个实际的解决方案。您需要检查是否已经看到该数字。您需要检查的是,数组中的每个数字都只出现在您的位置前面,而不是之前。您可以使用下面的代码执行此操作
public int uniques(int[] a) {
int unique = 1;
boolean seen = false;
for (int i = 1; i < a.length; i++) {
for (int j = 0; j < i; j++) {
if (a[i] == a[j]) {
seen = true;
}
}
if (!seen) {
unique++;
}
seen = false;
}
return unique;
}
public int uniques(int[]a){
int unique=1;
布尔值=假;
for(int i=1;i
在这段代码中,您正在迭代您看到的数字,并将其与您正在检查的数字进行比较(a[i]
)。您知道,要使其唯一,您以前无法看到它。好的,首先在您的解决方案中,您将返回设置为唯一值的int unique
。因此在您的示例中,它只返回1或4
接下来,关于一个实际的解决方案。您需要检查是否已经看到该数字。您需要检查的是,数组中的每个数字都只出现在您的位置前面,而不是之前。您可以使用下面的代码执行此操作
public int uniques(int[] a) {
int unique = 1;
boolean seen = false;
for (int i = 1; i < a.length; i++) {
for (int j = 0; j < i; j++) {
if (a[i] == a[j]) {
seen = true;
}
}
if (!seen) {
unique++;
}
seen = false;
}
return unique;
}
public int uniques(int[]a){
int unique=1;
布尔值=假;
for(int i=1;i
在这段代码中,您将迭代所看到的数字,并与正在检查的数字进行比较(
a[i]
)。您知道,要使其唯一,您以前无法看到它。这一种应该可以使用。我想这可能不是最优雅的方法,但它非常简单,只使用简单的数组。方法返回数组中的位数,但不计算重复数-我相信这就是您的目标
public int uniques(int[] a) {
int tempArray[] = new int[a.length];
boolean duplicate = false;
int index = 0;
int digitsAdded = 0;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < tempArray.length; j++) {
if (a[i] == tempArray[j]) {
duplicate = true;
}
}
if(!duplicate) {
tempArray[index] = a[i];
index++;
digitsAdded++;
}
duplicate = false;
}
//this loop is needed if you have '0' in your input array - when creating temp
//array it is filled with 0s and then any 0 in input is treated as a duplicate
//again - not most elegant solution, maybe I will find better later...
for(int i = 0; i < a.length; i++) {
if(a[i] == 0) {
digitsAdded++;
break;
}
}
return digitsAdded;
}
public int uniques(int[]a){
int tempArray[]=新的int[a.length];
布尔重复=假;
int指数=0;
int digitsAdded=0;
for(int i=0;i
这个方法应该可以使用。我想这可能不是最优雅的方法,但它非常简单,只使用简单的数组。方法返回数组中的位数,但不计算重复数-我相信这就是您的目标
public int uniques(int[] a) {
int tempArray[] = new int[a.length];
boolean duplicate = false;
int index = 0;
int digitsAdded = 0;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < tempArray.length; j++) {
if (a[i] == tempArray[j]) {
duplicate = true;
}
}
if(!duplicate) {
tempArray[index] = a[i];
index++;
digitsAdded++;
}
duplicate = false;
}
//this loop is needed if you have '0' in your input array - when creating temp
//array it is filled with 0s and then any 0 in input is treated as a duplicate
//again - not most elegant solution, maybe I will find better later...
for(int i = 0; i < a.length; i++) {
if(a[i] == 0) {
digitsAdded++;
break;
}
}
return digitsAdded;
}
public int uniques(int[]a){
int tempArray[]=新的int[a.length];
布尔重复=假;
int指数=0;
int digitsAdded=0;
for(int i=0;i