def solution(nums):
flag = 1
while flag:
i = 0
while i < len(nums):
for j in range(i + 1, len(nums)):
if nums[i] == nums[j]:
flag = 0
return nums[i]
if flag == 1:
i += 1
assert solution([2,3,1,0,2,5,3]) == 2
assert solution([2,3,1,0,5,3]) == 3
import org.junit.Test;
import java.util.*;
import static org.junit.jupiter.api.Assertions.*;
public class RepeatedNumber {
@Test
public void testCase() {
assertAll(()->{
assertEquals(repeatedNumber(new int[]{1, 0, 3, 2, 3, 4, 6, 7, 7}),3);
assertEquals(repeatedNumber(new int[]{2,3,1,0,2,5,3}),2);
assertEquals(repeatedNumber(new int[]{ 2, 3, 4, 6, 7, 7}),7);
assertEquals(repeatedNumber(new int[]{1, 0, 3, 2, 4, 6, 7, }),-1);
});
}
public int repeatedNumber(int[] list) {
Set<Integer> map = new HashSet<>();
int repeat = -1;
for (int num : list) {
if (!map.add(num)) {
repeat = num;
break;
}
}
return repeat;
}
}
def solution(nums: list):
# your code here
return [i for i in set(nums) if nums.count(i) > 1][0]
assert solution([2, 3, 1, 0, 2, 5, 3]) == 2
assert solution([5, 2, 4, 3, 3, 2, 1]) == 2
def solution(nums: list):
from collections import Counter
for value,count in Counter(nums).items():
if count >1:
return value
#思路:每个元素和他前面的元素对比,相同就返回。不同继续找后一个
def find_firstdulp(nums):
for i in range(len(nums)-1):
if nums[i+1] in nums[0:i+1]:
return nums[i+1]
nums = [2,3,3,2]
print(find_firstdulp(nums))
def find_num(li):
for i in li:
if li.count(i) > 1:
return i
public int reSeeMember(int[] memberList) {
int reSeeMember = 0;
List list =new ArrayList<>();
for (int i = 0; i < memberList.length; i++) {
for (int j = i+1; j < memberList.length; j++) {
if (memberList[i] == memberList[j]) {
//使用赋值的形式后面的会覆盖前面的内容。所以使用集合
list.add(memberList[i]);
}
}
}
//取集合中的第一个元素,第一个为最开始重复的数
reSeeMember = (int) list.get(0);
return reSeeMember;
}
@Test
void testCase_03(){
assertAll(()->{
assertEquals(reSeeMember(new int []{2,3,1,0,2,5,3}),2);
assertEquals(reSeeMember(new int []{23,45,2,45,67,8}),45);
assertEquals(reSeeMember(new int []{21,31,11,11,22,50,39}),11);
assertEquals(reSeeMember(new int []{222,32,13,3,2,5,3}),3);
});
}
def findfirstrepeatnum(M:list):
for i in range(len(M) - 1):
for j in range(1 + i, len(M)):
if M[i] == M[j]:
return M[i]
break
findfirstrepeatnum([3,3,1,0,2,5,3])==3
count=0
def fun(list1):
for i in list1:
global count
count=list1.count(i)
if count>1:
print(f’第一个重复的数据字是{i},重复次数:{count}')
break
fun([2,3,1,0,2,5,3])
def solution(nums: list):
for i in range(len(nums)):
if nums[i] in nums[:i]:
return nums[i]
li = [2,3,1,0,2,5,3]
t = {}
for i in li:
if t.get(i):
print(i)
break
else:
t[i] = 1
lst = [6,8,5,3,4,2,1,7,3,5,8,9]
def repeatedNum(lst):
for i in range(len(lst)):
for j in range(i+1,len(lst)):
if lst[i] == lst[j]:
print("第一组相同的数为:", lst[i])
break
else:
continue
break
repeatedNum(lst)
def double_num(nums):
dict={}
for i in range(len(nums)):
if nums[i] not in dict.keys():
dict[nums[i]]=1
else:
return nums[i]
assert double_num([2,3,1,0,2,5,3])==2
def find_first_repeat_number(arr: list):
my_set = set()
for i in arr:
init_len = len(my_set)
my_set.add(i)
if len(my_set) == init_len:
return i
public static void bubbleSort(int[] arr) {
LinkedHashMap<Integer, Integer> map = new LinkedHashMap<>();
for (int x : arr) {
int num = 0;
for (int y : arr) {
if (x == y) {
num += 1;
if (num > 1) {
map.put(x, num);
}
}
}
}
for (int i=1;i <= map.size();i++) {
if(i==1) {
System.out.println("第一个重复数字为:" + map.entrySet().stream().iterator().next().getKey());
}
}
}
def find_dup_number(num_list:list):
length = len(num_list)
for i in range(0, length-1):
for j in range(i+1, length):
if num_list[j] == num_list[i]:
print(f"第{i+1}个数字和第{j+1}和数字重复,重复的数字是了{num_list[i]}")
return {num_list[i]}
return None
l = [2, 3, 1, 1, 4, 4, 3, 9]
find_dup_number(l)
这个结果返回的是前n个数字中,首次出现重复的那个数。
如: l = [2, 3, 4, 4, 1, 1, 3, 9]
- 用这个方法会返回4
- 感觉题目的意思是返回3