home

WindowsXP batch


::blame: shardy@@differentchairs.com


@echo off


goto main

::
:: find a value in the given list that occurs an odd number of times
::
:findOddCount
  setlocal
  for /f "delims=:" %%e in ('echo %0') do set _self=$%%e
  for /f "tokens=1 delims==" %%e in ('set _bucket[ 2^>nul') do @set %e=

  :_counting
    if %1.==. goto _chkResult
    set /a _bucket[%1]+=1
    shift
    goto _counting

  :_chkResult
  for /f "tokens=2,3 delims==[]" %%e in ('set _bucket[ 2^>nul') do (
    set _foundOddCount=%%e
    for /f %%i in ('set /a %%f %%= 2') do (
      if %%i==1 goto _foc_done
      )
    )
  set _foundOddCount=

  :_foc_done
    endlocal & set %_self%=%_foundOddCount%
  goto:eof


:main
  setlocal

  set a1=1 1 2 2 3 3 4 4 5 5 6 7 7 7 7
  set a2=10 10 7 7 6 6  2 2 3 3 4 4 5 5 6 7 7 7 7 10 10
  set a3=6 6 10 10 7 7 6 6  2 2 3 3 4 4 5 5 6 7 7 7 7 10 10
  set a4=10 10 7 7  2 2 3 3 4 4 5 5 7 7 7 7 10 10 6
  set a5=6 6
  set a6=1


  call :findOddCount %a1%
  echo a1: [%$findOddCount%]

  call :findOddCount %a2%
  echo a2: [%$findOddCount%]

  call :findOddCount %a3%
  echo a3: [%$findOddCount%]

  call :findOddCount %a4%
  echo a4: [%$findOddCount%]

  call :findOddCount %a5%
  echo a5: [%$findOddCount%]

  call :findOddCount %a6%
  echo a6: [%$findOddCount%]

  endlocal
goto :eof