一个整型数组里除了两个数字只出现一次,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字。
数据范围:数组长度
,数组中每个数的大小 
要求:空间复杂度
,时间复杂度 )
要求:空间复杂度
提示:输出时按非降序排列。
function FindNumsAppearOnce( array ) {
// write code here
let a=[]
let map=new Map()
for(let i=0;i<array.length;i++){
if(map.get(array[i])==undefined) map.set(array[i],1);
else {
a.push(array[i])
}
}
let c=array.filter(function(item,index){
return !a.includes(item)
})
return c.sort((a,b)=>a-b)
}
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param array int整型一维数组
* @return int整型一维数组
*/
function FindNumsAppearOnce( array ) {
// write code here
let result = []
let res = array.reduce((temp,data)=>{
temp[data] = temp[data]? temp[data]+1:1
return temp
},{})
for (index in res){
if(res[index] == 1){
result.push(index)
}
}
return result.sort((a,b)=>{
return a-b
})
}
module.exports = {
FindNumsAppearOnce : FindNumsAppearOnce
}; function FindNumsAppearOnce( array ) {
// write code here
const map = new Map()
const res = []
for(const item of array){
if(map.has(item)){
let n = map.get(item)
map.set(item,n+1)
}else{
map.set(item,1)
}
}
map.forEach((value,key)=>{
if(value === 1)
res.push(key)
})
return res.sort()
} /**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param array int整型一维数组
* @return int整型一维数组
*/
function FindNumsAppearOnce( array ) {
// write code here
let len = array.length
let map = new Map()
let res = []
for(let i = 0; i <len; i++){
if(map.has(array[i])){
map.set(array[i],map.get(array[i])+1)
}
else map.set(array[i], 1)
}
map.forEach((value,key) => {
if(value ==1) res.push(key)
})
return res.sort((a,b) => a-b)
}
module.exports = {
FindNumsAppearOnce : FindNumsAppearOnce
}; /**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param array int整型一维数组
* @return int整型一维数组
*/
function FindNumsAppearOnce( array ) {
// write code here
var list = [];
var obj = {};
array.forEach(item=>{
if(obj[item]){
obj[item]++
}else{
obj[item]=1
}
})
let arr=[];
console.log(obj);
for(let i in obj){
if(obj[i]==1){
arr.push(i);
}
}
return arr;
}
module.exports = {
FindNumsAppearOnce : FindNumsAppearOnce
}; function FindNumsAppearOnce( array ) {
// write code here
let result=[];
while(array){
let del=array.pop();
if(array.indexOf(del)>-1){
array.splice(array.indexOf(del),1);
}
else{
result.push(del);
}
if(result.length>1) break;
}
if(result[1]<result[0]){
result.unshift(result.pop());
}
return result;
} pop和splice搭配,先pop出一个,用indexOf判断数组内还有没有,如果没有了说明就是只出现了一次的,把它保存在返回数组中,如果还有就说明出现了2次,使用splice删掉出现第二次出现的元素。直到返回数组result的长度大于1,跳出循环,并且给两个元素排序,得到最终结果。
function FindNumsAppearOnce( array ) {
// write code here
array.sort((a, b) => a - b);
let map = new Map();
for(let i = 0; i < array.length; i++){
if(map.has(array[i])){
map.set(array[i], map.get(array[i]) + 1);
}else{
map.set(array[i], 1);
}
}
let res = [];
map.forEach((val, key) => {
if(val === 1){
res.push(key);
}
});
return res;
}
module.exports = {
FindNumsAppearOnce : FindNumsAppearOnce
}; function FindNumsAppearOnce( array ) {
// write code here
var res = 0; //对整个数组求异或的结果
for(var i = 0; i < array.length; i++){
res ^= array[i];
}
var compare = 1;
while((compare & res) == 0){ //判断异或结果的二进制第一位是否为1,为1则直接跳过该循环
compare <<= 1; //为0则继续往后找,一直到找到为1的二进制位,该行代码也相当于compare *=2
}
var a = 0;
var b = 0;
for(var i = 0; i < array.length; i++){ //遍历数组,开始判断数字们的compare位是否为1
if((compare & array[i]) == 0){ //如果该数字二进制的第某位为0,则分到数组一
a ^= array[i]; //对数组一进行异或,得到a
}else{ //如果该数字二进制的第某位为1,则分到数组二
b ^= array[i]; //对数组二进行异或,得到b
}
}
return a < b ? [a,b] : [b,a];
} function FindNumsAppearOnce( array ) {
// write code here
array.sort();
var result = [];
var i = 0;
while(i < array.length){
if(array.indexOf(array[i]) != array.lastIndexOf(array[i])){
i = array.lastIndexOf(array[i]) + 1;
}else{
result.push(array[i]);
i++;
}
}
return result;
} 先把初始数组array排序,然后循环判断当前值的第一个索引和最后一个索引的值是不是相等,如果不等,说明出现不止一次,让i跳步,如果相等,说明只出现一次,是我们想要的值